-
Notifications
You must be signed in to change notification settings - Fork 288
/
Copy pathAC_dfs_n!.cpp
56 lines (48 loc) · 1.18 KB
/
AC_dfs_n!.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
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_dfs_n!.cpp
* Create Date: 2015-02-03 11:06:56
* Descripton: Just like I verson.
*/
#include <bits/stdc++.h>
using namespace std;
class Solution {
private:
// shared variable
int count;
vector<int> col; // columns placed queens
vector<int> m_diag; // main diag placed queens
vector<int> a_diag; // anti main diag placed queens
int N;
void dfs(int row) {
if (row == N) {
count++;
return;
}
for (int i = 0; i < N; ++i) {
if (col[i] || m_diag[row + i] || a_diag[row - i + N])
continue;
col[i] = m_diag[row + i] = a_diag[row - i + N] = 1;
dfs(row + 1);
col[i] = m_diag[row + i] = a_diag[row - i + N] = 0;
}
}
public:
int totalNQueens(int n) {
count = 0;
col = vector<int>(n, 0);
m_diag = vector<int>(n << 1, 0);
a_diag = vector<int>(n << 1, 0);
N = n;
dfs(0);
return count;
}
};
int main() {
int n;
Solution s;
cin >> n;
int ans = s.totalNQueens(n);
cout << ans << endl;
return 0;
}