BOJ - 13549 - 숨바꼭질 3

Updated:

from collections import deque

def bfs(N, K, dist):
    q = deque([N])
    dist[N] = 0
    while q:
        current = q.popleft()
        step = [current - 1, current + 1, current * 2]
        for dx in step:
            if 0 <= dx <= 100000 and dist[dx] == -1:
                if dx == current * 2:
                    q.appendleft(dx)
                    dist[dx] = dist[current]
                else:
                    q.append(dx)
                    dist[dx] = dist[current] + 1

def solution():
    N, K = map(int, input().split(' '))
    dist = [-1] * (100001)

    bfs(N, K, dist)
    print(dist[K])
solution()

https://www.acmicpc.net/problem/13549

Categories:

Updated:

Leave a comment