-
Notifications
You must be signed in to change notification settings - Fork 0
/
N-Queens2.java
56 lines (44 loc) · 1.63 KB
/
N-Queens2.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
/*
* Key point: dfs, much like the N-Queens
*/
public class Solution {
public int totalNQueens(int n) {
int result = 0;
if(n <= 0)
return result;
//using location[] to store the column of queen's location in each line
int location[] = new int[n];
result = dfsWorker(n, location, 0, 0);
return result;
}
//argument count store the number of solutions so far
public int dfsWorker(int n, int[] location, int count, int currentRow){
//reaching nth iteration, we find a solution
if(currentRow == n){
count++;
}
else{
for(int i = 0;i<n;i++){
location[currentRow] = i;
if(isValid(location, currentRow) == true){
count = dfsWorker(n, location, count, currentRow+1);
}
}
}
return count;
}
//valid whether the current location has collision with previous queens
public boolean isValid(int[] location, int currentRow){
for(int i = 0;i<currentRow;i++){
if(location[i] == location[currentRow])
return false;
//checking collision in diagonal line
int rowDistance = currentRow - i;
int columnDistance = location[currentRow] - location[i];
columnDistance = columnDistance > 0 ? columnDistance : -columnDistance;
if(rowDistance == columnDistance)
return false;
}
return true;
}
}