forked from ncduy0303/cses-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
53 lines (45 loc) · 1.02 KB
/
main.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
#include <bits/stdc++.h>
using namespace std;
#define ar array
#define ll long long
const int MAX_N = 1e5 + 1;
const ll MOD = 1e9 + 7;
const ll INF = 1e9;
int n;
vector<int> ans, vis;
string to_str(int x) {
string s;
for (int i = 0; i < n; i++) s += (x >> i & 1) ? "1" : "0";
reverse(s.begin(), s.end());
return s;
}
void backtrack(int pos, int x) {
vis[x] = 1;
ans.push_back(x);
if (pos == (1 << n) - 1) {
for (int nx : ans) cout << to_str(nx) << "\n";
exit(0);
}
for (int i = 0; i < n; i++) {
int nx = x ^ (1 << i);
if (vis[nx]) continue;
backtrack(pos + 1, nx);
vis[nx] = 0;
ans.pop_back();
}
}
void solve() {
cin >> n;
vis.assign(1 << n, 0);
backtrack(0, 0);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int tc = 1;
// cin >> tc;
for (int t = 1; t <= tc; t++) {
// cout << "Case #" << t << ": ";
solve();
}
}