/ ALGORITHM

백준 - 점프 점프(#11060)

백준 11060

image-20230110191442904

#BFS을 이용한 풀이법
from collections import deque

n = int(input())
jump = list(map(int, input().split()))
visited = [False]*n

def bfs(i, j):
    visited[0] = True   
    queue = deque([[i, j]])

    while queue:
        pos, cnt = queue.popleft()

        if pos == n - 1:
            return cnt

        for i in range(1, jump[pos] + 1):
            if pos + i < n and visited[pos + i] == False:
                visited[pos + i] = True
                queue.append([pos + i, cnt + 1])
    return -1


print(bfs(0, 0))
#Dynamic Programming을 이용한 풀이법
N = int(input())
jump = list(map(int,input().split()))

dp = [N]*(N)
dp[0]=0

stop=False
for i in range(N):
    for j in range(1,jump[i]+1):
        dp[i+j] = min(dp[i+j],dp[i]+1)
        if i+j==N-1:
            stop=True
            break
    if stop:
        break

print(dp[N-1] if dp[N-1]!=N else -1)