-
Notifications
You must be signed in to change notification settings - Fork 481
/
0051.cpp
67 lines (61 loc) · 1.65 KB
/
0051.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
#include <iostream>
#include <vector>
using namespace std;
static int x = [](){std::ios::sync_with_stdio(false);cin.tie(0);return 0;}();
class Solution
{
public:
vector<vector<string>> solveNQueens(int n)
{
col = vector<bool>(n, false);
dia1 = vector<bool>(2*n - 1, false);
dia2 = vector<bool>(2*n - 1, false);
vector<int> row;
putQueen(n, 0, row);
return result;
}
private:
vector<vector<string>> result;
vector<bool> col, dia1, dia2;
void putQueen(int n, int index, vector<int>& row)
{
if (index == n)
{
result.push_back(generateBoard(n, row));
return;
}
for (unsigned int i = 0; i < n; ++i)
{
if (!col[i] and !dia1[index + i] and !dia2[index - i + n - 1])
{
row.push_back(i);
col[i] = true; dia1[index + i] = true; dia2[index - i + n - 1] = true;
putQueen(n, index + 1, row);
col[i] = false; dia1[index + i] = false; dia2[index - i + n - 1] = false;
row.pop_back();
}
}
}
vector<string> generateBoard(int n, vector<int>& row)
{
vector<string> board(n, string(n, '.'));
for (unsigned int i = 0; i < n; ++i)
{
board[i][row[i]] = 'Q';
}
return board;
}
};
int main()
{
int n = 4;
vector<vector<string>> result = Solution().solveNQueens(n);
for (unsigned int i = 0; i < result.size(); ++i)
{
for (unsigned int j = 0; j < n; ++j)
{
cout << result[i][j] << endl;
}
cout << endl;
}
}