-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutation.cpp
More file actions
48 lines (36 loc) · 836 Bytes
/
permutation.cpp
File metadata and controls
48 lines (36 loc) · 836 Bytes
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
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <numeric>
#include <stack>
using namespace std;
typedef long long ll;
#define FOR(i, n) for (int i=0;i<n;++i)
#define INF(bit) (1 << bit)
void dfs(ll pos, ll n, vector<bool>& used)
{
if (pos == n) { cout << endl; return;}
FOR(i, n) if (!used[i]) {
used[i] = true;
cout << i << " ";
dfs(pos+1, n, used);
used[i] = false;
}
}
// permutation enumeration
int main()
{
ll n;
cin>>n;
vector<ll> a(n);
// initialization
FOR(i,n) a[i] = i;
vector<bool> used(n, false);
// Depth First Search (reccurent)
dfs(0, n, used);
// STL
do {
for (auto e:a) cout << e << " "; cout << endl;
} while( next_permutation(a.begin(), a.end()));
}