-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path[52]N 皇后 II.java
81 lines (69 loc) · 2.29 KB
/
[52]N 皇后 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
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
74
75
76
77
78
79
80
81
import java.util.Arrays;
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
int res = 0;
public int totalNQueens(int n) {
// base case
if (n <= 0) {
return 0;
}
char[][] board = new char[n][n];
for (char[] chars : board) {
Arrays.fill(chars, '.');
}
backtrack(board, 0);
return res;
}
void backtrack(char[][] board, int row) {
//结束回溯的条件
if (row == board.length) {
res++;
return;
}
int n = board[row].length;
for (int i = 0; i < n; i++) {
if (!isValid(board, row, i)) {
continue;
}
//做选择
board[row][i] = 'Q';
backtrack(board, row + 1);
//撤销选择
board[row][i] = '.';
}
}
List<String> chaToString(char[][] board) {
List<String> ans = new ArrayList<>();
StringBuilder sb = null;
for (int i = 0; i < board.length; i++) {
sb = new StringBuilder();
for (int j = 0; j < board[0].length; j++) {
sb.append(board[i][j]);
}
ans.add(sb.toString());
}
return ans;
}
private boolean isValid(char[][] board, int row, int col) {
int rows = board.length;
System.out.println(row +":" +col);
for (char[] chars : board) {
if (chars[col] == 'Q') {
return false;
}
}
// 检查右上角是否有皇后,右下脚还没走过肯定没有皇后
for (int i = row - 1, j = col + 1; i >= 0 && j < rows; i--, j++){
if (board[i][j] == 'Q') {
return false;
}
}
for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {
if (board[i][j] == 'Q') {
return false;
}
}
return true;
}
}
//leetcode submit region end(Prohibit modification and deletion)