Skip to content

Instantly share code, notes, and snippets.

@skysign
Created November 24, 2024 10:39
Show Gist options
  • Save skysign/02935e7e0da89ac0efab5d91b7df4099 to your computer and use it in GitHub Desktop.
Save skysign/02935e7e0da89ac0efab5d91b7df4099 to your computer and use it in GitHub Desktop.
from collections import deque
import sys
MAX = 100000 + 1
def solve():
input = sys.stdin.readline
N, K = map(int, input().strip().split())
q = deque()
q.append([N, 0]) # 수빈 현재위치, 시간
maps = [sys.maxsize for _ in range(MAX)]
while q:
cur, time = q.popleft()
if time < maps[cur]:
maps[cur] = time
if cur == K:
print(time)
return
else:
continue
for next in [cur * 2, cur - 1, cur + 1]:
if 0 <= next < MAX: # and visited[next] == False:
if next == cur * 2:
q.append([next, time])
else:
q.append([next, time + 1])
solve()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment