๊ณจ๋5 : BFS ๋ฌธ์ ์ด๋ค. ํ์ด BFS์์ ์ต๋จ๊ฑฐ๋ฆฌ๋ฅผ ์ฐพ์ ํ ๋ค์ ํ์์ ํ์ง ์๋ ๋ฐฉ๋ฒ Code from collections import deque import sys input = sys.stdin.readline MAXNUM = 1e6 N, K = map(int, input().split()) visited = [False] * int(MAXNUM + 1) minTime, minTimeNumOfVisited = 1e6, 0 def BFS(): global minTime, minTimeNumOfVisited q = deque() q.append((N, 0)) while q: pos, time = q.popleft() visited[pos] = True if time > minTime: continue # ์ ์ด์ ํ์ฌ๊ฑธ๋ฆฐ์๊ฐ์ด ์ต์์๊ฐ๋ณด๋ค ํฌ๋ฉด ํ์ํ ์ด์ ๊ฐ ์์ if pos == K: minTime = min(minTime, time) # ์ฒ์ ๋ฐฉ๋ฌธํ๋ฉด ์ฌ๊ธฐ์ ๊ฑธ๋ฆฌ๊ฒ ๋์ด์์ minTimeNumOfVisited += 1 nexts = [(pos - 1, time + 1), (pos + 1, time + 1), (2 * pos, time + 1)] for npos, ntime in nexts: if 0 <= npos <= MAXNUM and visited[npos] == False: q.append((npos, ntime)) BFS() print(minTime) print(minTimeNumOfVisited) Reference ๋ฐฑ์ค(12851๋ฒ) - ์จ๋ฐ๊ผญ์ง2