-
Notifications
You must be signed in to change notification settings - Fork 0
/
N-Queens-II
35 lines (34 loc) · 817 Bytes
/
N-Queens-II
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
struct place {
int row, col, right_diag, left_diag;
place():row(0), col(0), right_diag(0), left_diag(0) {}
place(int row, int col): row(row), col(col), right_diag(row - col), left_diag(row + col) {}
};
class Solution {
public:
int queen(int &count,int row,int n, vector<place>& vec) {
if (row == n)
{
count++;
return count;
}
for (int col = 0; col < n; col++) {
int flag = 1;
for (int i = 0; i < row; i++) {
if (vec[i].row == row || vec[i].col == col || vec[i].right_diag == (row - col) || vec[i].left_diag == (row + col)) {
flag = 0;
break;
}
}
if (flag) {
vec[row]=place(row, col);
queen(count, row + 1, n, vec);
}
}
return count;
}
int totalNQueens(int n) {
vector<place> vec(n);
int count=0;
return queen(count, 0,n, vec);
}
};