SWEA::3282 0/1 Knapsack


완전탐색을 하여 풀었고, 범위가 벗어나면 리턴을 통해 빠져나오도록 하였다.


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
#include <stdio.h>
#include <vector>
#define P pair<int,int>
using namespace std;
 
int t, n, k, res;
P d[101];
 
void dfs(int idx, int volume, int weight) {
    if (volume + d[idx].first > k) return;
 
    if (weight+d[idx].second > res) {
        res = weight + d[idx].second;
    }
 
    if (idx == n) return;
    dfs(idx + 1, volume, weight);
    dfs(idx + 1, volume + d[idx].first, weight + d[idx].second);
}
 
int main(){
    scanf("%d"&t);
    for (int tc = 1; tc <= t; tc++) {
        scanf("%d%d"&n, &k);
 
        res = 0;
 
        int x, y;
        for (int i = 0; i < n; i++) {
            scanf("%d %d"&x, &y);
            d[i] = P(x, y);
        }
 
        for (int i = 0; i < n; i++) {
            dfs(i, 00);
        }
 
        printf("#%d %d", tc, res);
    }
}
cs


'SWEA::문제풀이' 카테고리의 다른 글

3349 최솟값으로 이동하기  (0) 2018.02.23
1868 파핑파핑 지뢰찾기  (0) 2018.02.22
1949 등산로 조성  (0) 2018.02.13
2477 차량 정비소  (0) 2018.02.07
2115 벌꿀채취  (0) 2018.01.30

+ Recent posts