/
Solution.java
executable file
·101 lines (78 loc) · 2.91 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
public class Solution {
public void helper(int row, int total, List<List<String>> res, List<String> path,
boolean[] cols, boolean[] plus, boolean[] minus){
if(row == total){
res.add(path);
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;
List<String> newpath = new ArrayList<String>(path);
char[] tmpchar = new char[total];
for(int ii = 0; ii< tmpchar.length; ii++){
if(ii == i) tmpchar[ii] = 'Q';
else tmpchar[ii] = '.';
}
newpath.add(new String(tmpchar));
helper(row+1, total, res, newpath, cols, plus, minus);
cols[i] = false;
plus[row + i] = false;
minus[total + row - i] = false;
}
}
}
public List<List<String>> solveNQueens(int n) {
boolean[] cols = new boolean[n], plus = new boolean[2*n], minus = new boolean[2*n];
List<List<String>> res = new ArrayList<>();
helper(0, n, res, new ArrayList<String>(), cols, plus, minus);
return res;
}
}
public class Solution {
boolean[][] chessboard;
int target;
ArrayList<String[]> 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){
String[] valid = new String[target + 1];
for(int i = 0; i <= target ; i++){
char[] s = new char[target + 1];
for(int j = 0; j <= target; j++){
s[j] = chessboard[i][j] ? 'Q' : '.';
}
valid[i] = new String(s);
}
rt.add(valid);
return;
}
for(int i = 0; i <= target ; i++){
if(tryput(row, i)){
chessboard[row][i] = true;
search(row + 1);
chessboard[row][i] = false;
}
}
}
public List<String[]> solveNQueens(int n) {
chessboard = new boolean[n][n];
target = n - 1;
rt = new ArrayList<String[]>();
search(0);
return rt;
}
}