/
Solution.java
executable file
·86 lines (65 loc) · 2.17 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
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
82
83
84
85
86
public class Solution {
public int totalNQueens(int n) {
boolean[] cols = new boolean[n], plus = new boolean[2*n], minus = new boolean[2*n];
int[] res = new int[1];
res[0] = 0;
helper(0, n, res, cols, plus, minus);
return res[0];
}
public void helper(int row, int total, int[] res,
boolean[] cols, boolean[] plus, boolean[] minus){
if(row == total){
res[0]++;
return;
}
for(int i = 0; i < total; i++){
if(!cols[i] && !plus[row + i] && !minus[total + row - i]){
cols[i] = true;
plus[row + i] = true;
minus[total + row - i] = true;
helper(row+1, total, res, cols, plus, minus);
cols[i] = false;
plus[row + i] = false;
minus[total + row - i] = false;
}
}
}
}
public class Solution {
boolean[][] chessboard;
int target;
int rt;
boolean tryput(final int row, final int col){
for(int i = row - 1; i >= 0; i--){
int offset = row - i;
if(chessboard[i][col])
return false;
if(col - offset >= 0 && chessboard[i][col - offset])
return false;
if(col + offset <= target && chessboard[i][col + offset])
return false;
}
return true;
}
void search(final int row){
if( row > target){
rt++;
return;
}
for(int i = 0; i <= target ; i++){
if(tryput(row, i)){
chessboard[row][i] = true;
search(row + 1);
chessboard[row][i] = false;
}
}
}
public int totalNQueens(int n) {
// Note: The Solution object is instantiated only once and is reused by each test case.
chessboard = new boolean[n][n];
target = n - 1;
rt = 0;
search(0);
return rt;
}
}