문제
내가 생각한 풀이
크기가 100_001인 dist 배열을 만들고 -1로 초기화한다.
dist[N]의 값을 0으로 수정한다.
for문으로 순간이동하거나 걷는 거리를 돌며 방문하지 않은 좌표를 방문하고 dist 수정
코드 (Python)
import sys
from collections import deque
input = sys.stdin.readline
N, K = map(int, input().split())
dist = [-1] * 100_001
dist[N] = 0
queue = deque([N])
while queue:
xp = queue.popleft()
if xp == K:
print(dist[K])
exit(0)
for res in [xp*2, xp+1, xp-1]:
if not (0 <= res <= 100_000):
continue
if dist[res] == -1:
queue.append(res)
dist[res] = dist[xp] + 1
'BOJ' 카테고리의 다른 글
BOJ 1012. 유기농 배추 (Python) (0) | 2025.08.19 |
---|---|
BOJ 4179. 불! (Python) (0) | 2025.08.19 |
BOJ 7576. 토마토 (Python) (0) | 2025.08.19 |
BOJ 2178. 미로 탐색 (Python) (0) | 2025.08.19 |
BOJ 1926. 그림 (Python) (0) | 2025.08.19 |