티스토리 뷰

점프 가능한 곳으로 이동가능 한 최소값을 이용하여 다이나미그 프로그래밍으로 문제를 해결한다. 


import java.util.Arrays;
import java.util.Scanner;
/**
* 백준, 점프 점프, 11060
* 다이나믹 프로그래밍
*
* @author whitebeard-k
*
*/
public class Problem11060 {
static int N;
static int[] A;
static int[] dp;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
A = new int[N + 1];
for (int i = 1; i <= N; i++)
A[i] = sc.nextInt();
sc.close();
dp = new int[N + 1];
Arrays.fill(dp, Integer.MAX_VALUE);
// if (A[1] != 0)
dp[1] = 0;
for (int i = 1; i <= N; i++) {
if (dp[i] != Integer.MAX_VALUE) {
int jump = A[i];
for (int j = 1; j <= jump; j++) {
if (i + j > N)
continue;
dp[i + j] = Math.min(dp[i] + 1, dp[i + j]);
}
}
}
System.out.println(dp[N] == Integer.MAX_VALUE ? -1 : dp[N]);
}
}




반응형
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
글 보관함