코딩테스트/백준 알고리즘
[백준] 1697<DFS/BFS> 숨바꼭질 - python
zzangyeon
2022. 3. 24. 13:02
이게 왜 틀리지..
from collections import deque
start, goal = map(int,input().split())
q = deque()
q.append(start)
time = [0] * (goal+2)
def bfs():
while q:
x = q.popleft()
for i in (x-1, x+1, x*2):
if i == goal:
print(time[x] + 1)
return
if 0 <= i <= (goal+1):
if time[i] == 0:
time[i] = time[x] + 1
q.append(i)
if start > goal:
print(start - goal)
else:
bfs()