-
Notifications
You must be signed in to change notification settings - Fork 1
/
[leetcode]052-N-Queens II[递归回溯].cpp
62 lines (56 loc) · 1.46 KB
/
[leetcode]052-N-Queens II[递归回溯].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
/*
1.原题
https://leetcode.com/problems/n-queens-ii/
2. 思路
这题和上道题51思路是一样的,只不过,搜索到结果后不需要保存,
只需要计数就可以了。
*/
class Solution {
public:
int totalNQueens(int n) {
dim = n;
vector<string> mv(n, string(n, '.'));
dfs(mv, 0);
return num;
}
void dfs(vector<string> &mv, int row)
{
if (row == dim) //right answer
{
num++;
return;
}
for (int i = 0; i < dim; i++)
{
if (isValid(mv, row, i)) //current position is valid
{
mv[row][i] = 'Q';
dfs(mv, row+1); //process next row by recursion
mv[row][i] = '.';
}
}
return;
}
bool isValid(vector<string> &mv, int x, int y)
{
for (int i = 0; i < x; i++ ) // validate column data, here no need for row
{ // because we process row up to down
if (mv[i][y] != '.')
return false;
}
for (int i=x-1, j = y-1; i > -1 && j > -1; i--,j--) // master diagonal
{
if (mv[i][j] != '.')
return false;
}
for (int i = x-1, j = y+1; i>-1 && j<dim; i--,j++) // subdiagnonal
{
if (mv[i][j] != '.')
return false;
}
return true;
}
private:
int num{0};
int dim;
};