-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1718.cpp
More file actions
43 lines (42 loc) · 1.25 KB
/
1718.cpp
File metadata and controls
43 lines (42 loc) · 1.25 KB
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
class Solution {
public:
vector<int> res;
bool flag = false;
bool backtracking(int n, int m, int mask, vector<int>& temp, int curr) {
if (m == curr) {
if (flag) return true;
flag = true;
res = temp;
return true;
}
if (temp[curr] != -1) {
bool o = backtracking(n, m, mask, temp, curr + 1);
return o;
}
else {
for (int i = n; i >= 1; --i) {
if (mask & (1 << i)) {
continue;
}
if (i != 1 && curr + i >= m) continue;
if (i != 1 && temp[curr + i] != -1) continue;
temp[curr] = i;
if (i != 1) temp[curr + i] = i;
mask ^= (1 << i);
bool o = backtracking(n, m, mask, temp, curr + 1);
if (o) return true;
mask ^= (1 << i);
temp[curr] = -1;
if (i != 1) temp[curr + i] = -1;
}
}
return false;
}
vector<int> constructDistancedSequence(int n) {
int m = (n - 1) * 2 + 1;
vector<int> temp(m, -1);
int mask = 0;
backtracking(n, m, mask, temp, 0);
return res;
}
};