Algorithm

[SWEA] 5215. 햄버거 다이어트

프로그래민 2020. 5. 29. 13:28
반응형

knapsack 유형의 문제이다.

이 문제는 조합을 통하여 풀수 있는데 조합을 구현하는데 두가지 방법을 사용해보았다. 1번은 선택-비선택 재귀호출으로 구현하였고, 2번은 visit배열은 이용한 백트래킹 조합을 사용하여 구현하였다. 또한 두가지 방법모두 가지치기가 상당히 중요한 요소이므로 잘생각할 필요가 있다.

방법1 : 선택,비선택 재귀

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
package swea;
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
 
public class Solution_d3_5215_햄버거다이어트 {
    
    static int N;
    static int limit;
    static int[][] dp;
 
    static int[] grade;
    static int[] cal;
    static int answer;
    
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = null;
        
        int T=Integer.parseInt(br.readLine());
        
        for(int tc=1;tc<=T;tc++) {
            st=new StringTokenizer(br.readLine());
            N=Integer.parseInt(st.nextToken());
            limit=Integer.parseInt(st.nextToken());
            
            grade=new int[N];
            cal=new int[N];
            
            for(int i=0;i<N;i++) {
                st=new StringTokenizer(br.readLine());
                grade[i]=Integer.parseInt(st.nextToken());
                cal[i]=Integer.parseInt(st.nextToken());
            }
            answer=-1;
            dfs(0,0,0);
            System.out.println("#"+tc+" "+answer);
        }
    }
    
    static void dfs(int depth, int sumGrade, int sumCal) {
        if(sumCal>limit) {
            return;
        }
        if(depth==N) {
            answer=Math.max(answer, sumGrade);
            return;
        }
        dfs(depth+1,sumGrade+grade[depth], sumCal+cal[depth]);    //선택O
        dfs(depth+1,sumGrade,sumCal);                            //선택X
    }
}
 
                                     

방법2 : 백트래킹 조합

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
76
77
78
79
80
81
package swea;
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
 
public class Solution_d3_5215_햄버거다이어트_2 {
    
    static int N;
    static int limit;
    static int[][] dp;
 
    static int[] grade;
    static int[] cal;
    
    static int[] resultGrade;
    static int[] resultCal;
    
    static boolean[] visit;
    static int answer;
    
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = null;
        
        int T=Integer.parseInt(br.readLine());
        
        for(int tc=1;tc<=T;tc++) {
            st=new StringTokenizer(br.readLine());
            N=Integer.parseInt(st.nextToken());
            limit=Integer.parseInt(st.nextToken());
            
            grade=new int[N];
            cal=new int[N];
            
            resultGrade=new int[N];
            resultCal=new int[N];
            
            for(int i=0;i<N;i++) {
                st=new StringTokenizer(br.readLine());
                grade[i]=Integer.parseInt(st.nextToken());
                cal[i]=Integer.parseInt(st.nextToken());
            }
            answer=-1;
            visit=new boolean[N];
            dfs(0,0);
            System.out.println("#"+tc+" "+answer);
        }
        
    }
    
    static void dfs(int start, int depth) {
        
        int sumCal=0;
        int sumGrade=0;
        for(int i=0;i<depth;i++) {
            sumCal+=resultCal[i];
            sumGrade+=resultGrade[i];
        }
//        System.out.println(sumCal +","+ (depth));
        if(sumCal<=limit)
            answer=Math.max(answer, sumGrade);
        
        if(depth==N) {
//            System.out.println(Arrays.toString(resultCal));
            return;
        }
 
        for(int i=start;i<N;i++) {
            if(visit[i]==false) {
                visit[i]=true;
                resultCal[depth]=cal[i];
                resultGrade[depth]=grade[i];
                dfs(i,depth+1);
                visit[i]=false;
            }
        }
    }
}
 
                                       
반응형

'Algorithm' 카테고리의 다른 글

[BOJ] 17471. 게리맨더링  (0) 2020.05.30
[BOJ] 17069. 파이프 옮기기 2  (0) 2020.05.30
[SWEA] 3234. 준환이의 양팔저울  (0) 2020.05.29
[BOJ] 17142. 연구소 3  (0) 2020.05.29
[BOJ] 1987. 알파벳  (0) 2020.05.27