BOJ 1697. 숨바꼭질 (Python)

2025. 8. 19. 17:12·BOJ

문제

BOJ 1697

내가 생각한 풀이

크기가 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
'BOJ' 카테고리의 다른 글
  • BOJ 1012. 유기농 배추 (Python)
  • BOJ 4179. 불! (Python)
  • BOJ 7576. 토마토 (Python)
  • BOJ 2178. 미로 탐색 (Python)
rubato.dev
rubato.dev
  • rubato.dev
    rubato.dev
    rubato.dev
  • 전체
    오늘
    어제
    • 분류 전체보기 (8) N
      • BOJ (6) N
      • 알고리즘 (0)
      • TIL (2)
      • Project (0)
  • 인기 글

  • 태그

    js
    PYTHON
    BOJ
    TIL
    CSS
    BFS
  • 최근 글

  • hELLO· Designed By정상우.v4.10.4
rubato.dev
BOJ 1697. 숨바꼭질 (Python)
상단으로

티스토리툴바