source: https://leetcode.com/problems/n-queens-ii/
C/C++ Solution to LeetCode problem 52. N-Queens II.
Problem
The n-queens puzzle is the problem of placing n
queens on an n x n
chessboard such that no two queens attack each other.
Given an integer n
, return the number of distinct solutions to the n-queens puzzle.
Examples
Example 1:
Input: n = 4
Output: 2
Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above.
Example 2:
Input: n = 1
Output: 1
Constraints
1 <= n <= 9
Solution
Same solution as for problem 51.
The only difference is that, instead of saving the solution, we just increase a counter.
Solution 1:
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
class Solution {
private:
int result = 0;
bool isValid(int n, int r, int c, set<pair<int, int>>& tmpSol) {
for (const auto& pair: tmpSol) {
if (pair.first == r || pair.second == c)
return false;
if (abs(pair.first - r) == abs(pair.second - c))
return false;
}
return true;
}
void solve(int n, set<pair<int, int>>& tmpSol, int r) {
if (r == n) {
result ++;
return;
}
for (int c=0; c<n; c++) {
if (!isValid(n, r, c, tmpSol))
continue;
tmpSol.insert({r, c});
solve(n, tmpSol, r+1);
tmpSol.erase({r, c});
}
}
public:
int totalNQueens(int n) {
set<pair<int,int>> tmpSol;
solve(n, tmpSol, 0);
return result;
}
};
Solution 2:
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
class Solution {
private:
int result = 0;
bool isValid(int n, vector<string>& tmpSol, int row, int col){
for (int i=0; i<n; i++){
if (tmpSol[i][col] == 'Q')
return false;
}
for(int i=row-1, j=col-1; i>=0 && j>=0; i--, j--){
if(tmpSol[i][j] == 'Q')
return false;
}
for (int i=row-1, j=col+1; i>=0 && j<n; i--, j++) {
if(tmpSol[i][j] == 'Q')
return false;
}
return true;
}
void solve(int n, vector<string>& tmpSol, int r){
if (r == n) {
result ++;
return;
}
for (int c=0; c<n; c++) {
if (isValid(n, tmpSol, r, c)) {
tmpSol[r][c] = 'Q';
solve(n, tmpSol, r+1);
tmpSol[r][c] = '.';
}
}
}
public:
int totalNQueens(int n) {
vector<string> tmpSol(n , string(n, '.'));
solve(n, tmpSol, 0);
return result;
}
};