-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathleetcode_0051_N-Queens.java
More file actions
90 lines (81 loc) · 2.81 KB
/
leetcode_0051_N-Queens.java
File metadata and controls
90 lines (81 loc) · 2.81 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// AC: Runtime: 5 ms, faster than 49.79% of Java online submissions for N-Queens.
// Memory Usage: 39.6 MB, less than 47.44% of Java online submissions for N-Queens.
// backtracking
// T:O(n^3), S:O(n^2)
//
class Solution {
public List<List<String>> solveNQueens(int n) {
List<List<Integer>> record = new LinkedList<>();
int[][] chessboard = new int[n][n];
backtracking(chessboard, new LinkedList<>(), record, 0);
List<List<String>> ret = new LinkedList<>();
for (List<Integer> item: record) {
List<String> tempList = new LinkedList<>();
for (int index: item) {
StringBuilder rowStr = new StringBuilder();
for (int i = 0; i < n; i++) {
if (i == index) {
rowStr.append("Q");
} else {
rowStr.append(".");
}
}
tempList.add(rowStr.toString());
}
ret.add(tempList);
}
return ret;
}
private void backtracking(int[][] chessboard, List<Integer> path, List<List<Integer>> out, int startIndex) {
int size = chessboard.length;
List<Integer> pathCopy = new LinkedList<>(path);
if (pathCopy.size() == size) {
out.add(pathCopy);
return;
}
for (int i = 0; i < size; i++) {
if (checkChessboard(chessboard, startIndex, i)) {
pathCopy.add(i);
chessboard[startIndex][i] = 1;
backtracking(chessboard, pathCopy, out, startIndex + 1);
pathCopy.remove(pathCopy.size() - 1);
chessboard[startIndex][i] = 0;
}
}
}
private boolean checkChessboard(int[][] chessboard, int row, int col) {
int size = chessboard.length;
// check column
for (int i = 0; i < size; i++) {
if (i == row) {
continue;
}
if (chessboard[i][col] == 1) {
return false;
}
}
// check 45° diagonal
for (int i = row - 1, j = col + 1; i >= 0 && j < size; i--, j++) {
if (chessboard[i][j] == 1) {
return false;
}
}
for (int i = row + 1, j = col - 1; i < size && j >= 0; i++, j--) {
if (chessboard[i][j] == 1) {
return false;
}
}
// check 135° diagonal
for (int i = row + 1, j = col + 1; i < size && j < size; i++, j++) {
if (chessboard[i][j] == 1) {
return false;
}
}
for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {
if (chessboard[i][j] == 1) {
return false;
}
}
return true;
}
}