DevYoon
[๋ฐฑ์ค] 13549. ์จ๋ฐ๊ผญ์ง3 (Python) ๋ณธ๋ฌธ
link ๐ https://www.acmicpc.net/problem/13549
13549๋ฒ: ์จ๋ฐ๊ผญ์ง 3
์๋น์ด๋ ๋์๊ณผ ์จ๋ฐ๊ผญ์ง์ ํ๊ณ ์๋ค. ์๋น์ด๋ ํ์ฌ ์ N(0 ≤ N ≤ 100,000)์ ์๊ณ , ๋์์ ์ K(0 ≤ K ≤ 100,000)์ ์๋ค. ์๋น์ด๋ ๊ฑท๊ฑฐ๋ ์๊ฐ์ด๋์ ํ ์ ์๋ค. ๋ง์ฝ, ์๋น์ด์ ์์น๊ฐ X์ผ
www.acmicpc.net
๐ซ ๊ฑธ์ด์ ๊ฐ ๋๋ณด๋ค ์๊ฐ์ด๋์ผ๋ก ๊ฐ ๋ ์๊ฐ์ด ๋ ์ ๊ฒ ๊ฑธ๋ฆผ โก๏ธ 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)