-
Notifications
You must be signed in to change notification settings - Fork 0
/
N Queens.java
53 lines (46 loc) · 1.67 KB
/
N Queens.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
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
class Solution {
public List<List<String>> solveNQueens(int n) {
HashSet<Integer> columns = new HashSet<>();
HashSet<Integer> forwardDiags = new HashSet<>();
HashSet<Integer> backwardDiags = new HashSet<>();
List<List<String>> res = new ArrayList<>();
char[][] cur = new char[n][n];
for (int row = 0; row < n; ++row) {
for (int col = 0; col < n; ++col) {
cur[row][col] = '.';
}
}
dfs(n, columns, forwardDiags, backwardDiags, cur, res, 0);
return res;
}
private void dfs(int n, HashSet<Integer> columns,
HashSet<Integer> forwardDiags,
HashSet<Integer> backwardDiags, char[][] cur,
List<List<String>> res, int row) {
if (row == n) {
List<String> board = new ArrayList<>();
for (int i = 0; i < n; ++i) {
board.add(new String(cur[i]));
}
res.add(board);
}
for (int col = 0; col < n; ++col) {
if (columns.contains(col) || forwardDiags.contains(col - row) ||
backwardDiags.contains(col + row)) {
continue;
}
cur[row][col] = 'Q';
columns.add(col);
forwardDiags.add(col - row);
backwardDiags.add(col + row);
dfs(n, columns, forwardDiags, backwardDiags, cur, res, row + 1);
columns.remove(col);
forwardDiags.remove(col - row);
backwardDiags.remove(col + row);
cur[row][col] = '.';
}
}
}