황소개발자

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

백준 문제 풀이

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

hjp845 2020. 3. 3. 07:02
반응형

불가능이란 없다

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

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

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