-
반응형
https://www.acmicpc.net/problem/11060
11060번: 점프 점프
재환이가 1×N 크기의 미로에 갇혀있다. 미로는 1×1 크기의 칸으로 이루어져 있고, 각 칸에는 정수가 하나 쓰여 있다. i번째 칸에 쓰여 있는 수를 Ai라고 했을 때, 재환이는 Ai이하만큼 오른쪽으로
www.acmicpc.net
풀이
DP를 이용해서 문제를 해결했다. 먼저 해당지점에서 점프했을때, dp[next]보다 더 작은횟수로 도달할 수 있다면 갱신을 해준다. 이때 점프를 했을때 범위를 넘어가면 안되므로 조건을 추가해준다.
코드
import java.util.Arrays; import java.util.Scanner; public class Main { static int[] v; static int[] dp; public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); v=new int[n]; dp=new int[n]; for(int i=0;i<n;i++){ v[i]=sc.nextInt(); } sc.close(); Arrays.fill(dp,Integer.MAX_VALUE); dp[0]=0; for(int i=0;i<n;i++){ if(dp[i]==Integer.MAX_VALUE) continue; int canGo=v[i]+i; for(int j=i+1;j<=canGo&&j<n;j++){ dp[j]=Math.min(dp[j],dp[i]+1); } } System.out.println(dp[n-1]==Integer.MAX_VALUE?-1:dp[n-1]); } }
반응형'문제풀이 > 백준oj' 카테고리의 다른 글
[백준OJ] 16943번 숫자 재배치 (0) 2021.10.28 [백준OJ] 2670번 연속부분최대곱 (0) 2021.10.28 [백준OJ] 4256번 트리 (0) 2021.09.28 [백준OJ] 20366번 같이 눈사람 만들래? (0) 2021.09.27 [백준OJ] 11559번 Puyo Puyo (0) 2021.09.26 댓글