Backjoon/bfs
[bfs] 1697_숨바꼭질
casylm
2022. 9. 25. 00:58
code
from collections import deque
n,k =map(int,input().split())
m = [0 for i in range(100001)]
q = deque()
q.append(n)
direction = [-1,1,2]
def bfs():
while q:
cur = q.popleft()
if cur == k:
print(m[k])
return
for i in range(3):
if i == 2:
nn = cur * direction[i]
else:
nn = cur + direction[i]
if 0<=nn<=100000:
if not m[nn]:
m[nn] = m[cur] + 1
q.append(nn)
bfs()
if cur == k:
print(m[k])
return
부분을 상단으로 올리니 런타임 오류 해결
왜,,?