알고리즘

<백준> 1697번 파이썬 알고리즘

changha. 2021. 10. 29. 22:52
from collections import deque

def bfs():
  q = deque()
  q.append(n)
  while q:
    x = q.popleft()
    if x == k:
      print(visit[x])
      break
    for tx in (x - 1, x + 1, x * 2):
      if 0 <= tx <= MAX and not visit[tx]:
        visit[tx] = visit[x] + 1
        q.append(tx)

MAX = 10 ** 5
visit = [0] * (MAX + 1)
n, k = map(int, input().split())

bfs()