본문 바로가기

Programming/BOJ

[백준/Python] 백준 1697번 숨바꼭질

from collections import deque

N, K = map(int, input().split())
visit = [False]*100001


cur = N
time = 0

q = deque([[cur,time]])
while q:
    v = q.popleft()
    cur, time = v[0], v[1]
    
    if visit[cur] == False:
        if cur == K:
            print(time)
            break
        visit[cur] = True
        
        if cur + 1 <= 100000:
            q.append([cur+1, time+1])
        if cur - 1 >= 0:
            q.append([cur-1, time+1])
        if cur*2 <= 100000:
            q.append([cur*2, time+1])