-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path52. N-Queens II.c
81 lines (65 loc) · 1.74 KB
/
52. N-Queens II.c
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
52. N-Queens II
Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.
*/
#define IDX(I, J, SZ) ((I) * (SZ) + J)
void set_flags(int *buff, int i, int j, int n, int flag) {
int k, x, y;
buff[IDX(i, j, n)] = flag;
for (k = 0; k < n; k ++) { // row
if (k != j && buff[IDX(i, k, n)] != -1) {
buff[IDX(i, k, n)] += flag ? 1 : -1;
}
}
for (k = 0; k < n; k ++) { // col
if (k != i && buff[IDX(k, j, n)] != -1) {
buff[IDX(k, j, n)] += flag ? 1 : -1;
}
x = k;
y = j - i + k;
if (y >= 0 && y < n) {
if (x != i && y != j && buff[IDX(x, y, n)] != -1) {
buff[IDX(x, y, n)] += flag ? 1 : -1;
}
}
y = j + i - k;
if (y >= 0 && y < n) {
if (x != i && y != j && buff[IDX(x, y, n)] != -1) {
buff[IDX(x, y, n)] += flag ? 1 : -1;
}
}
}
}
void bt(int n, int *k, int *buff, int row) {
int col;
if (row == n) {
// all done
(*k) ++;
return;
}
for (col = 0; col < n; col ++) {
if (buff[IDX(row, col, n)] == 0) {
set_flags(buff, row, col, n, -1);
bt(n, k, buff, row + 1);
set_flags(buff, row, col, n, 0);
}
}
}
int totalNQueens(int n) {
int *buff, k = 0;
buff = calloc(n * n, sizeof(int));
//assert(result && buff);
bt(n, &k, buff, 0);
free(buff);
return k;
}
/*
Difficulty:Hard
Total Accepted:64.8K
Total Submissions:144.4K
Companies Zenefits
Related Topics Backtracking
Similar Questions
N-Queens
*/