DevYoon
[๋ฐฑ์ค] 13913. ์จ๋ฐ๊ผญ์ง4 (Python) ๋ณธ๋ฌธ
link ๐ https://www.acmicpc.net/problem/13913
๐ซ ๊ฐ์ฅ ๋นจ๋ฆฌ ๋์์ ์ฐพ๋ ์๊ฐ๊ณผ ๊ทธ ๋ฃจํธ๋ฅผ ์ถ๋ ฅํด์ฃผ์ด์ผ ํ๋ค.
๐ซ check ๋ฐฐ์ด์ ์์ผ๋ก ๋ค์ด๊ฐ ๊ฐ์ ์์น์ ํ์ฌ ์์น๋ฅผ ์ ์ด์ฃผ์๊ณ , ์ญ์ถ์ ํด์ฃผ์๋ค.
from collections import deque
def BFS(start, target):
q = deque()
q.append((start, 0)) # start, cnt
Mincnt = 0
Minpath = []
check = [-1]*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] == -1:
check[p] = now # check[์์ผ๋ก ๊ฐ ์์น]์ ํ์ฌ ์์น ๋ฃ์ด์ฃผ๊ธฐ
q.append((p, cnt+1))
now = target
for _ in range(Mincnt+1): # ๋ฃจํธ ์ญ์ผ๋ก ์ถ์ฒ
Minpath.append(now)
now = check[now]
return Mincnt, Minpath[::-1]
n, k = map(int, input().split())
res = BFS(n, k)
print(res[0])
print(*res[1])