코테 공부

[DP]점프 점프(백준 11060번), 자바

DaEun_ 2022. 6. 29. 12:12

11060번: 점프 점프 (acmicpc.net)

 

11060번: 점프 점프

재환이가 1×N 크기의 미로에 갇혀있다. 미로는 1×1 크기의 칸으로 이루어져 있고, 각 칸에는 정수가 하나 쓰여 있다. i번째 칸에 쓰여 있는 수를 Ai라고 했을 때, 재환이는 Ai이하만큼 오른쪽으로

www.acmicpc.net

고려해야 하는 반례

#1

input

3

0 3 4

output: -1

 

#2.

input

5

1 0 0 1 0 

output: -1

import java.util.*;

public class Main2 {
	
	
	public static void main(String[] args){
	
		Scanner sc=new Scanner(System.in);
		int N;
		N=sc.nextInt();
		int[]dp=new int[N];
		int[] d=new int[N];
		for(int i=0;i<N;i++) d[i]=sc.nextInt();
		dp[0]=1;
		if(d[0]!=0) {
			for(int i=0;i<N;i++) {
				if(d[i]==0 || dp[i]==0) continue;
				for(int j=1;j<=d[i] && i+j<N;j++ ) {
					if(dp[i+j]==0) dp[i+j]=(dp[i]+1);
					else dp[i+j]=Math.min(dp[i]+1,dp[i+j]);
				}
			}
		}
		System.out.println(dp[N-1]-1);
	}
}