DevYoon
[๋ฐฑ์ค] 13549. ์จ๋ฐ๊ผญ์ง3 (Python) ๋ณธ๋ฌธ
link ๐ https://www.acmicpc.net/problem/13549
๐ซ ๊ฑธ์ด์ ๊ฐ ๋๋ณด๋ค ์๊ฐ์ด๋์ผ๋ก ๊ฐ ๋ ์๊ฐ์ด ๋ ์ ๊ฒ ๊ฑธ๋ฆผ โก๏ธ appendleft๋ก ๋จผ์ ๋ฃ์ด์ฃผ๊ธฐ
from collections import deque
def BFS(start, target):
q = deque()
q.append((start, 0)) # start, cnt
Mincnt = 0
check = [0]*100001
while q:
now, cnt = q.popleft()
if now == target:
Mincnt = cnt
break
path = [now-1, now+1, now*2]
for p in path:
if 0<= p <= 100000 and check[p] == 0:
check[p] = 1
if p == now*2:
q.appendleft((p, cnt))
else:
q.append((p, cnt+1))
return Mincnt
n, k = map(int, input().split())
res = BFS(n, k)
print(res)