-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path51NQueens.java
67 lines (56 loc) · 2 KB
/
51NQueens.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
57
58
59
60
61
62
63
64
65
66
67
/**
* 51. N-Queens
* https://leetcode.com/problems/n-queens/
*/
class Solution {
/**
* Approach 1. Backtracking
* Time: O(N!), Space: O(N^2)
*/
public List<List<String>> solveNQueens(int n) {
char[][] board = createBoard(n);
List<List<String>> output = new ArrayList<>();
backtrack(board, 0, n, output, new HashSet<Integer>(), new HashSet<Integer>(), new HashSet<Integer>());
return output;
}
private void backtrack(char[][] board, int r, int n, List<List<String>> output, Set<Integer> cols, Set<Integer> diags, Set<Integer> antiDiags) {
if (r == n) {
output.add(cloneBoard(board, n)); // goal
return;
}
for (int c = 0; c < n; ++c) {
int diagonal = r - c;
int antiDiagonal = r + c;
// if the queen is not placeable
if (cols.contains(c) || diags.contains(diagonal) || antiDiags.contains(antiDiagonal)) {
continue;
}
// "Add" the queen to the board
cols.add(c);
diags.add(diagonal);
antiDiags.add(antiDiagonal);
board[r][c] = 'Q';
// Move on to the next row with the updated board state
backtrack(board, r + 1, n, output, cols, diags, antiDiags);
// "Remove" the queen from the board since we have already
// explored all valid paths using the above function call
cols.remove(c);
diags.remove(diagonal);
antiDiags.remove(antiDiagonal);
board[r][c] = '.';
}
}
private char[][] createBoard(int n) {
char[][] board = new char[n][n];
for (int r = 0; r < n; ++r)
Arrays.fill(board[r], '.');
return board;
}
private List<String> cloneBoard(char[][] board, int n) {
List<String> result = new ArrayList<>();
for (int r = 0; r < n; ++r) {
result.add(new String(board[r]));
}
return result;
}
}