-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNo51NQueens.java
73 lines (65 loc) · 1.92 KB
/
No51NQueens.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
68
69
70
71
72
73
package com.wzx.leetcode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author wzx
* @see <a href="https://leetcode.com/problems/n-queens/">https://leetcode.com/problems/n-queens/</a>
*/
public class No51NQueens {
private boolean[] colSet = null;
private boolean[] diagonalSet = null;
private boolean[] antiDiagonalSet = null;
/**
* 回溯
* <p>
* time: O(n!)
* space: O(n)
*/
public List<List<String>> solveNQueens(int n) {
// 初始化棋盘
List<char[]> board = new ArrayList<>(n);
for (int i = 0; i < n; i++) {
char[] row = new char[n];
Arrays.fill(row, '.');
board.add(row);
}
// 每列是否有皇后
colSet = new boolean[n];
// 每个左上斜线是否有皇后
diagonalSet = new boolean[2 * n - 1];
// 每个右上斜线是否有皇后
antiDiagonalSet = new boolean[2 * n - 1];
List<List<String>> res = new LinkedList<>();
recursion(0, n, board, res);
return res;
}
private void recursion(int row, int n, List<char[]> board, List<List<String>> res) {
if (row == n) {
List<String> solution = board.stream()
.map(String::new)
.collect(Collectors.toList());
res.add(solution);
return;
}
for (int col = 0; col < n; col++) {
if (valid(row, col, n)) {
board.get(row)[col] = 'Q';
setVisit(row, col, n, true);
recursion(row + 1, n, board, res);
board.get(row)[col] = '.';
setVisit(row, col, n, false);
}
}
}
private boolean valid(int row, int col, int n) {
return !colSet[col] && !diagonalSet[col + row] && !antiDiagonalSet[row - col + n - 1];
}
private void setVisit(int row, int col, int n, boolean visit) {
colSet[col] = visit;
diagonalSet[col + row] = visit;
antiDiagonalSet[row - col + n - 1] = visit;
}
}