-
Notifications
You must be signed in to change notification settings - Fork 3
/
52.cpp
29 lines (29 loc) · 1014 Bytes
/
52.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
class Solution {
public:
int res = 0;
void backtracking(int n, int row, vector<bool>& cols, vector<bool>& diagonals, vector<bool>& antidiagonals) {
if (n == row) {
res++;
return;
}
for (int col = 0; col < n; ++col) {
if (cols[col]) continue;
if (diagonals[row - col + n - 1]) continue;
if (antidiagonals[row + col]) continue;
cols[col] = true;
diagonals[row - col + n - 1] = true;
antidiagonals[row + col] = true;
backtracking(n, row + 1, cols, diagonals, antidiagonals);
cols[col] = false;
diagonals[row - col + n - 1] = false;
antidiagonals[row + col] = false;
}
}
int totalNQueens(int n) {
vector<bool> cols(n, false);
vector<bool> diagonals(n * 2 - 1, false);
vector<bool> antidiagonals(n * 2 - 1, false);
backtracking(n, 0, cols, diagonals, antidiagonals);
return res;
}
};