-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNQueens2.java
More file actions
59 lines (47 loc) · 1.58 KB
/
Copy pathNQueens2.java
File metadata and controls
59 lines (47 loc) · 1.58 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
59
// The n-queens puzzle is the problem of placing n queens on an n×n chessboard
// such that no two queens attack each other.
// Given an integer n, return the number of distinct solutions
// to the n-queens puzzle.
// See: https://leetcode.com/problems/n-queens-ii/
package leetcode.backtracking;
import java.util.HashSet;
import java.util.Set;
public class NQueens2 {
private Set<Integer> rDiag = new HashSet<>();
private Set<Integer> lDiag = new HashSet<>();
private Set<Integer> hor = new HashSet<>();
private Set<Integer> ver = new HashSet<>();
private int ans = 0;
public int totalNQueens(int n) {
backtrack(n, 0);
return ans;
}
private void backtrack(int n, int row) {
if (row == n) {
ans++;
return;
}
for (int col = 0; col < n; col++) {
if (isAvailable(row, col)) {
// Mark all
rDiag.add(row + col);
lDiag.add(row - col);
hor.add(row);
ver.add(col);
backtrack(n, row + 1);
// Unmark all
rDiag.remove(row + col);
lDiag.remove(row - col);
hor.remove(row);
ver.remove(col);
}
}
}
private boolean isAvailable(int row, int col) {
return !rDiag.contains(row + col) && !lDiag.contains(row - col) && !hor.contains(row) && !ver.contains(col);
}
public static void main(String[] args) {
NQueens2 sln = new NQueens2();
System.out.println(sln.totalNQueens(4));
}
}