-
Notifications
You must be signed in to change notification settings - Fork 288
/
AC_dfs_n!.cpp
57 lines (52 loc) · 1.34 KB
/
AC_dfs_n!.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
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_dfs_n!.cpp
* Create Date: 2015-01-01 10:45:58
* Descripton: dfs, choose or not choose
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 0;
class Solution {
private:
void dfs(vector<vector<int> > &ans, vector<int> &single,
vector<int> &candi, int cur, int rest) {
int sz = candi.size();
if (sz <= cur || rest < 0)
return;
if (rest == 0) {
ans.push_back(single);
return;
}
// choose cur
single.push_back(candi[cur]);
dfs(ans, single, candi, cur, rest - candi[cur]);
single.pop_back();
// don't choose cur
dfs(ans, single, candi, cur + 1, rest);
}
public:
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
vector<vector<int> > ans;
vector<int> single;
sort(candidates.begin(), candidates.end());
dfs(ans, single, candidates, 0, target);
return ans;
}
};
int main() {
int tar;
int n;
Solution s;
cin >> n >> tar;
vector<int> v(n);
for (int i = 0; i < n; i++)
cin >> v[i];
vector<vector<int> > res = s.combinationSum(v, tar);
for (auto &i : res) {
for (auto &j : i)
cout << j << ' ';
puts("");
}
return 0;
}