BaekJoon 14501, 퇴사 dp or dfs


이문제는 dp로도 풀 수있고  dfs로도 풀 수 있는 문제입니다.

https://www.acmicpc.net/problem/14501

요즘 DP문제만 엄청나게 풀고 있는데, 아무리 해도 점화식 세우는게 어렵네요 ㅠ

계속 풀다보면 익숙해지겠죠?



 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.util.ArrayList;
import java.util.Scanner;

public class BaekJoon14501 {
    static int N, max = 0;
    static int[] T, P;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Pair[] list;
        N = sc.nextInt();T = new int[N + 1];
        P = new int[N + 1];list = new Pair[N + 1];

        for (int i = 1; i < N + 1; i++) {
            T[i] = sc.nextInt();P[i] = sc.nextInt();
            list[i] = new Pair(i, P[i]);
        }
        for (int i = 1; i < list.length; i++) {
            dfs(list[i]);
        }

        System.out.println(max);
    }

    public static void dfs(Pair l) {
            if (l.day + T[l.day] < N + 1) {
                for (int j = l.day + T[l.day]; j < N + 1; j++) {
                    if (j + T[j] <= N + 1) {
                        dfs(new Pair(j, l.pay + P[j]));
                    } else {
                        max = Math.max(l.pay, max);
                    }
                }
            }else if(l.day + T[l.day] == N + 1) {
                max = Math.max(l.pay, max);return;
            }
    }
}
class Pair implements Comparable<Pair> {
    int day, pay;
    Pair(int day, int pay) {
        this.day = day; this.pay = pay;
    }
    @Override
    public int compareTo(Pair o) {
        return 0;
    }
}

이 블로그의 인기 게시물

웹툰 무료로 볼 수 있는 사이트

BackJoon 1011, Fly me to the alpha centauri, 규칙 찾기 문제

BaekJoon 14503, 로봇 청소기