-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path全排列.cpp
47 lines (39 loc) · 957 Bytes
/
全排列.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
#include<iostream>
#include<vector>
using namespace std;
///保存中间结果
vector<char> path;
///保存所有结果
vector<vector<char> > result;
void dfs(vector<char>& vv, int step, int flag[]) {
if(step == vv.size()) {
// for(int i=0; i<path.size(); i++)
// cout << path[i] << " ";
// cout << endl;
result.push_back(path);
return;
}
for(int i=0; i<vv.size(); i++) {
if(flag[i] == 0) {
flag[i] = 1;
path.push_back(vv[i]);
dfs(vv, step+1, flag);
path.pop_back();
flag[i] = 0;
}
}
}
int main() {
vector<char> v;
for(int i=0; i<3; i++)
v.push_back('o' + i);
///标志位
int flag[v.size()];
memset(flag, 0, sizeof(flag));
dfs(v, 0, flag);
for(int i=0; i<result.size(); i++) {
for(int j=0; j<result[i].size(); j++)
cout << result[i][j] << " ";
cout << endl;
}
}