forked from yuanx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNQueens.java
More file actions
58 lines (49 loc) · 1.2 KB
/
NQueens.java
File metadata and controls
58 lines (49 loc) · 1.2 KB
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
public class Solution {
public ArrayList<String[]> solveNQueens(int n) {
// Start typing your Java solution below
// DO NOT write main() function
ArrayList<String[]> re = new ArrayList<String[]>();
if(n<=0) return re;
int[][] place = new int[n][n];
placeQueens(re,place,n,0);
return re;
}
public void placeQueens(ArrayList<String[]> re, int[][] place, int n, int level){
if(level == n){
re.add(shape(place,n));
return;
}
for(int i=0; i<n; i++){
if(canplace(level,i,place,n)){
place[level][i] = 1;
placeQueens(re,place,n,level+1);
place[level][i] = 0;
}
}
}
public boolean canplace(int i, int j, int[][] place, int n){
for(int k=0; k<n; k++){
if(k==i) continue;
if(place[k][j]==1) return false;
for(int m=0; m<n; m++){
if(Math.abs(k-i)==Math.abs(m-j) && place[k][m] == 1)
return false;
}
}
return true;
}
public String[] shape(int[][] p, int n){
String[] strs = new String[n];
for(int i=0; i<n; i++){
StringBuilder temp = new StringBuilder();
for(int j=0; j<n; j++){
if(p[i][j]==0)
temp.append(".");
else
temp.append("Q");
}
strs[i]=temp.toString();
}
return strs;
}
}