전공공부
[BOJ_22869] 징검다리 건너기 (small) 본문
설명
저는 DP 형태의 메모리제이션을 활용하여서 문제를 풀었습니다. 보시다 싶이 문제 조건만 맞추면 풀 수 있는 문제라서 따로 풀이가 필요하지는 않을 듯하고 메모리제이션을 적절히 사용하는 것이 포인트 일 것 같습니다.
코드
package BOJ.Dynamic_Programming;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class BOJ_22869 {
public static void main(String[] args) throws Exception{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer tk = new StringTokenizer(in.readLine());
int N = Integer.parseInt(tk.nextToken());
int K = Integer.parseInt(tk.nextToken());
tk = new StringTokenizer(in.readLine());
int[] A = new int[N];
int[] dp = new int[N];
for (int i = 0; i < N; i++){
A[i] = Integer.parseInt(tk.nextToken());
}
dp[0] = 1;
for (int i = 0; i < N - 1; i++){
if(dp[i] == 1) {
for (int j = i + 1; j < N; j++) {
if (((j - i) * (1 + Math.abs(A[i] - A[j]))) <= K) {
dp[j] = 1;
}
}
}
}
if(dp[N - 1] == 0)
{
System.out.println("NO");
}
else {
System.out.println("YES");
}
}
}
22869번: 징검다리 건너기 (small)
$N$개의 돌이 일렬로 나열 되어 있다. $N$개의 돌에는 왼쪽부터 차례대로 수 $A_{1} A_{2} ... A_{i} ... A_{N}$로 부여되어 있다. 가장 왼쪽에 있는 돌에서 출발하여 가장 오른쪽에 있는 돌로 건너가려고
www.acmicpc.net
'Study > Problem Solving' 카테고리의 다른 글
[BOJ_2293] 동전1 (0) | 2024.03.24 |
---|---|
[BOJ_1106] 호텔 (0) | 2024.03.23 |
[BOJ_1965] 상자 넣기 (0) | 2024.03.14 |
[BOJ_20152] Game Addiction (0) | 2024.03.12 |
[BOJ_2876] 그래픽스 퀴즈 (0) | 2024.03.11 |