-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0051-n-queens.java
More file actions
46 lines (46 loc) · 1.49 KB
/
0051-n-queens.java
File metadata and controls
46 lines (46 loc) · 1.49 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
class Solution {
public List<List<String>> solveNQueens(int n) {
List<List<String>> res = new ArrayList<>();
String [] board = new String[n];
String s = ".";
s = s.repeat(n);
Arrays.fill(board, s);
dfsQ(res, n, 0, board);
return res;
}
public boolean isValid(String[] board, int x, int y, int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == x || j == y || Math.abs(i - x) == Math.abs(j - y)) {
if (board[i].charAt(j) != '.') return false;
}
}
}
return true;
}
public void dfsQ(List<List<String>> res, int QueensLeft, int row, String[] board) {
if (QueensLeft == 0) {
List<String> path = new ArrayList<>();
for (String st: board) {
path.add(st);
}
res.add(path);
}
if (row >= board.length) {
return;
}
for (int j = 0; j < board.length; j++) {
if (isValid(board, row, j, board.length)) {
String prevRow = board[row];
String newRow = "";
for (int k = 0; k < board.length; k++) {
if (k == j) newRow += 'Q';
else newRow += '.';
}
board[row] = newRow;
dfsQ(res, QueensLeft - 1, row + 1, board);
board[row] = prevRow;
}
}
}
}