-
Notifications
You must be signed in to change notification settings - Fork 5
/
52. N-Queens II.java
29 lines (25 loc) · 1.14 KB
/
52. N-Queens II.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
class Solution {
private int totalSolutions = 0;
public int totalNQueens(int n) {
boolean[] cols = new boolean[n];
boolean[] rightDiagonal = new boolean[2 * n];
boolean[] leftDiagonal = new boolean[2 * n];
this.dfs(0,cols, leftDiagonal, rightDiagonal, n);
return this.totalSolutions;
}
private void dfs(int currow, boolean[] cols, boolean[] leftDiagonal, boolean[] rightDiagonal, int n) {
// TODO Auto-generated method stub
if (currow == n) this.totalSolutions++;
for (int col = 0; col < n; col++) {
int id1 = currow + col; // basically leftDiagonalPosition
// (row = 1, col = 0 -> id1 = 4 ==> 0 - 1 + 5 = 4 ) // basically rightDiagonalPosition
int id2 = col - currow + n;
if(cols[col] || leftDiagonal[id1] || rightDiagonal[id2]) continue;
cols[col] = leftDiagonal[id1] = rightDiagonal[id2] = true;
dfs(currow + 1, cols, leftDiagonal, rightDiagonal, n);
cols[col] = leftDiagonal[id1] = rightDiagonal[id2] = false;
}
}
}