-
Notifications
You must be signed in to change notification settings - Fork 286
Expand file tree
/
Copy pathNQueen.java
More file actions
65 lines (57 loc) · 1.82 KB
/
NQueen.java
File metadata and controls
65 lines (57 loc) · 1.82 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
class Solution {
public List<List<String>> solveNQueens(int n) {
List<List<String>> ans = new ArrayList<List<String>>();
char[][] chess = new char[n][n];
// . stands for empty position
// Q stands for occupied position
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
chess[i][j] = '.';
}
}
placeQueens(ans, chess, 0);
return ans;
}
private void placeQueens(List<List<String>> ans, char[][] chess, int row) {
if (row == chess.length) {
ans.add(build(chess));
return;
}
for (int col = 0; col < chess.length; col++) {
// Check if current position is valid
if (valid(chess, row, col)) {
chess[row][col] = 'Q';
placeQueens(ans, chess, row + 1);
chess[row][col] = '.';
}
}
}
private boolean valid(char[][] chess, int row, int col) {
// check all rows for the current column
for (int i = 0; i < row; i++) {
if (chess[i][col] == 'Q') {
return false;
}
}
//check for 45 degree positions
for (int i = row - 1, j = col + 1; i >= 0 && j < chess.length; i--, j++) {
if (chess[i][j] == 'Q') {
return false;
}
}
//check for 135 positions
for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {
if (chess[i][j] == 'Q') {
return false;
}
}
return true;
}
private List<String> build(char[][] chess) {
List<String> path = new ArrayList<>();
for (int i = 0; i < chess.length; i++) {
path.add(new String(chess[i]));
}
return path;
}
}