284. 숨바꼭질 2

아현·2021년 8월 30일
0

Algorithm

목록 보기
297/400

백준




1. Python


from collections import deque
n, k = map(int, input().split())
MAX = 100001
time = [0] * MAX
cnt = 0

def dfs():
    global cnt
    q = deque()
    q.append(n)
    while q:
        v = q.popleft()
        if v == k: 
            cnt += 1
        for next in (v - 1, v + 1, v * 2):
            if 0 <= next < MAX:
                if time[next] >= time[v] + 1 or not time[next]:
                    time[next] = time[v] + 1
                    q.append(next)

dfs()

print(time[k])
print(cnt)
profile
Studying Computer Science

0개의 댓글