-
Notifications
You must be signed in to change notification settings - Fork 368
/
Copy pathN-Queens II.java
29 lines (26 loc) · 932 Bytes
/
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 {
public int totalNQueens(int n) {
return backtrack(0, new HashSet<>(), new HashSet<>(), new HashSet<>(), n);
}
private int backtrack(int row, Set<Integer> diagonals, Set<Integer> antiDiagonals, Set<Integer> columns, int n) {
if (row == n) {
return 1;
}
int possibleSolutions = 0;
for (int col = 0; col < n; col++) {
int currDiagonal = row - col;
int currAntiDiagonal = row + col;
if (columns.contains(col) || diagonals.contains(currDiagonal) || antiDiagonals.contains(currAntiDiagonal)) {
continue;
}
columns.add(col);
diagonals.add(currDiagonal);
antiDiagonals.add(currAntiDiagonal);
possibleSolutions += backtrack(row + 1, diagonals, antiDiagonals, columns, n);
columns.remove(col);
diagonals.remove(currDiagonal);
antiDiagonals.remove(currAntiDiagonal);
}
return possibleSolutions;
}
}