황소개발자

백준 1697 파이썬 python : 숨바꼭질 @@황소처럼 우직하게@@ 본문

백준 문제 풀이

백준 1697 파이썬 python : 숨바꼭질 @@황소처럼 우직하게@@

hjp845 2020. 3. 3. 05:17
반응형

화이팅!

n, k = map(int, input().split())

dist = [-1 for i in range(100001)]

from collections import deque

def bfs(v):
    q = deque()
    q.append(v)
    dist[v] = 0
    while q:
        now = q.popleft()
        if now + 1 <= 100000 and dist[now + 1] == -1:
            q.append(now + 1)
            dist[now + 1] = dist[now] + 1
        if now - 1 >= 0 and dist[now - 1] == -1:
            q.append(now - 1)
            dist[now - 1] = dist[now] + 1
        if now * 2 <= 100000 and dist[now * 2] == -1:
            q.append(now * 2)
            dist[now * 2] = dist[now] + 1
bfs(n)
print(dist[k])
반응형
Comments