-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackpackRecursion.cpp
More file actions
54 lines (48 loc) · 1.06 KB
/
backpackRecursion.cpp
File metadata and controls
54 lines (48 loc) · 1.06 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*its eaay to use dp and recursion to deal with backpack problem,
*but no easy to save intermediate results.
*we use the idea of a binary tree to return the intermediate path.
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <list>
using namespace std;
struct Result {
int num;
vector<int> t;
};
Result result{0, {}};
Result dp(int a[], int n, int w) {
if (n < 0 ) {
Result s{0, {}};
return s;
}
if (a[n] > w) {
Result tmp = dp(a, n - 1, w);
tmp.t.push_back(0);
return tmp;
}
else {
//cout<<"a"<<endl;
Result r1 = dp(a, n - 1, w);
Result r2 = dp(a, n - 1, w - a[n]);
if(r2.num + 1 >= r1.num) {
r2.t.push_back(1);
r2.num++;
return r2;
}
else {
r1.t.push_back(0);
return r1;
}
}
}
int main() {
int a[6] = {2,7,3,4,9,6};
int w = 7 ;
Result aaa = dp(a, 5, w);
cout<< aaa.num<<endl;
for (auto item : aaa.t)
cout<<item<<' ';
return 1;
}