728x90

 

파이썬

 

실3 바이러스

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import sys
def dfs(graph, root):
    visited[root] = True
    for i in graph[root]:
        if not visited[i]:
            dfs(graph, i)
input = sys.stdin.readline
computer = int(input())
connectedPair = int(input())
graph = {}
for i in range(connectedPair):
    a, b = map(int, input().split())
    try:
        graph[a].append(b)
    except:
        graph[a] = [b]
    try:
        graph[b].append(a)
    except:
        graph[b] = [a]
visited = [False* (computer + 1)
dfs(graph, 1)
print(visited.count(True- 1)
cs

 

 

실1 미로 탐색

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import sys
from collections import deque
input = sys.stdin.readline
N, M = map(int, input().split())
maze = []
for i in range(N):
    maze.append(list(map(int, input().rstrip())))
 
dx = [-1100]
dy = [00-11]
 
visited = []
queue = deque()
queue.append((00))
 
while queue:
    x, y = queue.popleft()
 
    for i in range(4):
        nx = x + dx[i]
        ny = y + dy[i]
 
        if nx >= N or ny >= M or nx < 0 or ny < 0 or maze[nx][ny] == 0:
            continue
        if maze[nx][ny] == 1:
            maze[nx][ny] = maze[x][y] + 1
            queue.append((nx, ny))
 
print(maze[N-1][M-1])
cs

 

728x90
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기