평범한 그래프 탐색.
from collections import deque
def BFS(start,goal):
queue = deque()
queue.append([start,0])
visited = {i: False for i in range(1,V+1)}
visited[start] = True
while queue:
cur_node, cur_dist = queue.popleft()
if cur_node==goal:
return cur_dist
for next_node in graph[cur_node]:
if not visited[next_node]:
visited[next_node] = True
queue.append([next_node,cur_dist+1])
return 0
for test in range(1,int(input())+1):
V, E = map(int, input().split())
graph = {i: [] for i in range(1,V+1)}
for _ in range(E):
a, b = map(int, input().split())
graph[a] += [b]
graph[b] += [a]
S, G = map(int, input().split())
answer = BFS(S, G)
print(f'#{test} {answer}')
'practivceAlgorithm > swexpertacademy' 카테고리의 다른 글
[SWEA][Python] 5105 미로의 거리 (0) | 2021.08.20 |
---|---|
[SWEA][Python] 5099 피자 굽기 (0) | 2021.08.20 |
[SWEA][Python] 5097 회전 (0) | 2021.08.20 |
[SWEA][Python] 2005 파스칼의 삼각형 (0) | 2021.08.19 |
[SWEA][Python] 1234 비밀번호 (0) | 2021.08.19 |