-
Notifications
You must be signed in to change notification settings - Fork 2
/
n_queens2.cc
55 lines (49 loc) · 1.29 KB
/
n_queens2.cc
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
// Follow up for N-Queens problem.
//
// Now, instead outputting board configurations, return the total number of distinct solutions.
/**
* To get the orignin n_queens code to pass the large test in leetcode, we need a little more optimization here.
*/
class Solution {
public:
int totalNQueens(int n) {
vector<int> arr(n, 0);
for (int i = 0; i < n; i++) {
arr[i] = i;
}
int result = 0;
dfs(result, arr, 0);
return result;
}
void dfs(int &result, vector<int> &arr, int n) {
if (n == arr.size()) {
result++;
return;
}
for (int i = n; i < arr.size(); i++) {
swap(&arr[n], &arr[i]);
/* we don't have to dfs all the possibilities. */
if (valid(arr, n)) {
dfs(result, arr, n + 1);
}
swap(&arr[n], &arr[i]);
}
}
void swap(int *a, int *b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
}
bool valid(vector<int> &arr, int n) {
for (int i = 0; i < n; i++) {
if (abs(arr[i] - arr[n]) == abs(i - n)) {
return false;
}
}
return true;
}
int abs(int n) {
return n > 0? n : -n;
}
};