-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathknapsack.h
More file actions
36 lines (31 loc) · 1.11 KB
/
knapsack.h
File metadata and controls
36 lines (31 loc) · 1.11 KB
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
//
// knapsack.h
// backtracking
//
// Created by junl on 2019/7/25.
// Copyright © 2019 junl. All rights reserved.
//
#ifndef knapsack_hpp
#define knapsack_hpp
#include <stdio.h>
/**
0-1背包问题. 对于一组不同质量,不可分割的物品,我们需要选择一些放入背包中,在满足最大重量限制的条件下,背包中所能放入的最大值是多少?
*/
void backpack01_bt(int weights[], int n, int level, int ctweight,int limitweight, int *result);
int backpack01(int weights[], int n, int limitweight){
int result = 0;
backpack01_bt(weights, n, 0, 0, limitweight, &result);
return result;
}
void backpack01_bt(int weights[], int n,int level, int ctweight,int limitweight, int *result){
if (level == n || *result == limitweight){
if (*result < ctweight){
*result = ctweight;
}
return;
}
backpack01_bt(weights, n, level+1, ctweight, limitweight, result);
if (ctweight + weights[level] <= limitweight)
backpack01_bt(weights, n, level+1, ctweight + weights[level], limitweight, result);
}
#endif /* knapsack_hpp */