-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
50 lines (39 loc) · 1.06 KB
/
Solution.java
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
package _0052_N_Queens_II;
import java.util.List;
/**
* Date: 2020/9/3 15:15
* Content:
*/
public class Solution {
int totalNum;
boolean[] col;
boolean[] dia1;
boolean[] dia2;
private void totalNQueens(int n, int index) {
if (index == n) {
totalNum++;
return;
}
for (int j = 0; j < n; j++)
if (!col[j] && !dia1[index + j] && !dia2[index - j + n - 1]) {
col[j] = true;
dia1[index + j] = true;
dia2[index - j + n - 1] = true;
totalNQueens(n, index + 1);
col[j] = false;
dia1[index + j] = false;
dia2[index - j + n - 1] = false;
}
}
public int totalNQueens(int n) {
col = new boolean[n];
dia1 = new boolean[2 * n - 1];
dia2 = new boolean[2 * n - 1];
totalNQueens(n, 0);
return totalNum;
}
public static void main(String[] args) {
int n = 4;
System.out.println((new Solution()).totalNQueens(n));
}
}