-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathp051.cpp
83 lines (80 loc) · 2.08 KB
/
p051.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
class Solution {
public:
int *place;
bool *y0;
bool *yx;
bool *y_x;
string emptyboard;
void f(vector<vector<string>> &result, const int n, const int p)
{
int i;
if (n == p)
{
vector<string> str(n, emptyboard);
for (i = 0; i < n; i++)
{
str[i][place[i]] = 'Q';
}
result.push_back(str);
if (place[0] < n>>1)
{
vector<string> str;
for (i = 0; i < n; i++)
{
str.push_back(emptyboard);
}
for (i = 0; i < n; i++)
{
str[i][n-1-place[i]] = 'Q';
}
result.push_back(str);
}
return;
}
for (i = 0; i < n; i++)
{
if ((!y0[i]) && (!yx[i+p]) && (!y_x[i-p+(n-1)]))
{
place[p] = i;
y0[i] = true;
yx[i+p] = true;
y_x[i-p+(n-1)] = true;
f(result, n, p+1);
y_x[i-p+(n-1)] = false;
yx[i+p] = false;
y0[i] = false;
}
}
}
vector<vector<string>> solveNQueens(int n) {
vector<vector<string>> result;
if (n == 0) return result;
place = new int[n];
y0 = new bool[n];
yx = new bool[(n<<1)+1];
y_x = new bool[(n<<1)+1];
int i;
emptyboard = "";
for (i = 0; i < n; i++)
{
emptyboard += ".";
y0[i] = false;
}
for (i = 0; i < (n<<1)+1; i++)
{
yx[i] = y_x[i] = false;
}
for (i = 0; i < (n+1)>>1; i++)
{
place[0] = i;
y0[i] = true;
yx[i] = true;
y_x[i+n-1] = true;
f(result, n, 1);
y_x[i+n-1] = false;
yx[i] = false;
y0[i] = false;
}
return result;
}
};