본문 바로가기

BOJ/JAVA

자두나무

 

//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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
public class BOJ_2240 {
 
    /*
     * 1에서 떨어질 때 받아먹으려면 내가 움직인 횟수가 짝수번,
 
        2에서 떨어질 때 홀수번이라고 생각했습니다.
        
        그외에는 받아먹지못하게 처리르헀고요.
        
     */
    static int t, w, max_eat;
    static int dp[][][] = new int[1001][32][3]; //t초에 i번 혹은 j번에 있을 때 자두의 최대개수 
    static int move[] = new int[1001];
    
    public static void main(String[] args) throws IOException{
        // TODO Auto-generated method stub
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st= new StringTokenizer(br.readLine());
        
        t = Integer.parseInt(st.nextToken());
        w = Integer.parseInt(st.nextToken());
        
        for (int i = 1; i <= t; i++) {
            st = new StringTokenizer(br.readLine());
            move[i] = Integer.parseInt(st.nextToken()); //자두가 떨어지는 위치 
            
        }
        //input 
        
        //자두의 현재 위치는 1번에 있다. 
        
//        for (int i = 0; i < t; i++) {
//            dp[i][1] = 1 == move[i] ? 1 : 0; //같은 위치에 있을 때는 자두를 먹을 수 있다.  
//            //0 1 1 0 0 1 1 (1에만 계속 있었을 때 얻을 수 있는 최대 자두 개수)
//        }
//        
        for (int i = 1; i <= t; i++) {
            for (int j = 0; j <= w; j++) {
                dp[i][j][1= dp[i][j][2= -1;
            }
        }
        
        int ans = Math.max(moveJadoo(101) , moveJadoo(112));
        
        
        System.out.println(ans);
    }
    static int moveJadoo(int times, int move_cnt, int cur) {
        if (times > t || move_cnt > w ) { 
            return 0//그만 움직임 
        }
        
        if (dp[times][move_cnt][cur] != -1return dp[times][move_cnt][cur];
        //이미 했음 
        
        if (cur == move[times]) {
            //자두를 먹을 수 있다. 
            dp[times][move_cnt][cur] = 1;
        }
        else {
            //움직여야 먹을 수 있다. 
            dp[times][move_cnt][cur] = 0;
        }
        
        dp[times][move_cnt][cur] += Math.max(moveJadoo(times+1, move_cnt, cur), moveJadoo(times+1, move_cnt+13-cur));
        
        return dp[times][move_cnt][cur];
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

'BOJ > JAVA' 카테고리의 다른 글

영역구하기  (0) 2020.02.17
부분합  (0) 2020.02.17
한 줄로 서기  (0) 2020.02.16
좋은 수열  (0) 2020.02.16
숨바꼭질4  (0) 2020.02.16