-
Notifications
You must be signed in to change notification settings - Fork 0
/
BackTracking.cpp
43 lines (33 loc) · 1.05 KB
/
BackTracking.cpp
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
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
void print(vector<int> arr){
for(auto ele: arr){
cout << ele;
}
cout <<"\n";
}
bool BoundFunction(int &i,vector<int> &subset, vector<int> &weights, int &limit){
return (accumulate(subset.begin(), subset.end(),0) + weights[i] <= limit) && (accumulate(subset.begin(), subset.end(),0) + accumulate(weights.begin()+i, weights.end(),0) >= limit);
}
void backtracking(int i, vector<int> subset, vector<int> weights, int limit ){
if(accumulate(subset.begin(), subset.end(),0) == limit){
print(subset);
return;
}
if(BoundFunction(i, subset, weights, limit)){
subset.push_back(weights[i]);
backtracking(i+1, subset, weights, limit);
subset.pop_back();
backtracking(i+1, subset, weights, limit);
}
}
int main()
{
vector<int> weights{2,2,4};
int sum{4};
vector<int> subset;
backtracking(0, subset, weights, sum);
}