-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode-N-QueensII.cpp
More file actions
46 lines (34 loc) · 1.1 KB
/
LeetCode-N-QueensII.cpp
File metadata and controls
46 lines (34 loc) · 1.1 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
43
44
45
46
class Solution {
bool can_place(int x, int y, vector<vector<int>>& board) {
int n = board.size();
pair<int, int> moves[8] = {{1, 1}, {1, -1}, {-1, 1}, {-1, -1}, {1, 0}, {0, 1}, {-1, 0}, {0, -1}};
for (const auto& m: moves) {
int i = x;
int j = y;
while (i >= 0 && i < n && j >= 0 && j < n) {
if (board[i][j]) return false;
i += m.first;
j += m.second;
}
}
return true;
}
int count(vector<vector<int>>& board, int k) {
int n = board.size();
int c = 0;
if (k == n) return 1;
for (int i = 0; i < n; ++i) {
if (can_place(i, k, board)) {
board[i][k] = 1;
c += count(board, k + 1);
board[i][k] = 0;
}
}
return c;
}
public:
int totalNQueens(int n) {
vector<vector<int>> board(n, vector<int>(n));
return count(board, 0);
}
};