-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLeetcode51.java
52 lines (50 loc) · 1.85 KB
/
Leetcode51.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
class Leetcode51{
public List<List<String>> solveNQueens(int n) {
List<List<String>> res = new ArrayList<>();
int[] col = new int[n];
if(n <= 0)return res;
dfs(0,col,res);
return res;
}
public void dfs(int row,int[] col, List<List<String>> res){
int n = col.length;
if(row == n){
// we solve the last queen
// expand current col into line
List<String> list = new ArrayList<>();
for(int i = 0;i < row;i++){
// for each line find its queen position
StringBuilder sb = new StringBuilder();
for(int j = 0;j < col.length;j++){
// ith queen is at (i,col[i]);
if(col[i] == j)sb.append('Q');
// other will just be empty
else sb.append('.');
}
// add current line into matrix
list.add(sb.toString());
}
// add current matrix into result
res.add(new ArrayList<>(list));
}else{
for(int i = 0;i < n;i++){
col[row] = i;
if(isValid(col,row)){
dfs(row+1,col,res);
}
}
}
}
private boolean isValid(int[] col,int row){
for(int i = 0;i < row;i++){
// for every col, check if there is a queen in the same col.
// because every queen must occpy a row.
// so we assume queen i will take i row;
// wee need to check every queen before current queen
// to check diaganal line, the diff between row and col should the same
// if they in a diaganal
if(col[row] == col[i] || Math.abs(row-i) == Math.abs(col[row]-col[i]))return false;
}
return true;
}
}