https://www.acmicpc.net/problem/16928
import sys
from collections import deque
input = sys.stdin.readline
N, M = map(int, input().split()) # N: 사다리 개수, M: 뱀 개수
board = list(range(101))
visited = [-1] * 101
# 사다리
for _ in range(N):
x, y = map(int, input().split())
board[x] = y
# 뱀
for _ in range(M):
u, v = map(int, input().split())
board[u] = v
def bfs():
queue = deque([(1, 0)]) # (현재 위치, 이동 횟수)
visited[1] = 1
while queue:
cur, moves = queue.popleft()
if cur == 100:
return moves
for dice in range(1, 7):
nxt = cur + dice
if nxt <= 100 and not visited[nxt]:
visited[nxt] = 1
queue.append((board[nxt], moves + 1))
print(bfs())
board는 딕셔너리말고 리스트로 설정하는 것이 메모리, 속도, 가독성이 모두 좋다!
'백준' 카테고리의 다른 글
[Python/파이썬] 백준 13144. List of Unique Numbers (0) | 2025.04.01 |
---|---|
백준 2493. 탑 (0) | 2025.02.23 |
백준 11501. 주식 (0) | 2025.02.07 |
백준 20006. 랭킹전 대기열 (0) | 2025.02.06 |
백준 20055. 컨테이너 벨트 위의 로봇 (0) | 2025.02.03 |