백준 11060 점프 점프 (re)

gmlwlswldbs·2021년 9월 29일
0

코딩테스트

목록 보기
39/130
from collections import deque
n = int(input())
a = list(map(int, input().split()))
check = [-1] * len(a)

q = deque()
q.append(0)
check[0] = 0
while q:
    x = q.popleft()
    for i in range(1, a[x] + 1):
        y = x + i
        if y >= len(a):
            continue
        if check[y] == -1:
            q.append(y)
            check[y] = check[x] + 1

print(check[-1])

bfs 로 풀 수 있다

n = int(input())
a = list(map(int, input().split()))
check = [-1] * n

check[0] = 0

for i in range(n):
    if check[i] == -1:
        continue
    for j in range(1, a[i] + 1):
        if i + j >= n:
            break
        if check[i + j] == -1 or check[i] + 1 < check[i + j]:
            check[i + j] = check[i] + 1

print(check[-1])

dp..

0개의 댓글