-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathN_Queens.java
97 lines (88 loc) · 2.95 KB
/
N_Queens.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import java.util.ArrayList;
import java.util.List;
public class N_Queens {
/**
* Solves the N-Queens problem.
*
* @param n The size of the chessboard.
* @return A list of possible solutions.
*/
public List<List<String>> solveNQueens(int n) {
// Initialize the board with '.' indicating empty cells
char[][] board = new char[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
board[i][j] = '.';
}
}
List<List<String>> res = new ArrayList<>();
dfs(board, 0, res);
return res;
}
/**
* Uses Depth-First Search to recursively place queens on the board.
*
* @param board The current board state.
* @param colIndex The current column index.
* @param res The results list to store valid board states.
*/
private void dfs(char[][] board, int colIndex, List<List<String>> res) {
// If the end of the board is reached, add the board state to results
if (colIndex == board.length) {
res.add(construct(board));
return;
}
// Try placing a queen in each row of the current column
for (int i = 0; i < board.length; i++) {
if (validate(board, i, colIndex)) {
board[i][colIndex] = 'Q'; // Place the queen
dfs(board, colIndex + 1, res);
board[i][colIndex] = '.'; // Revert - backtrack
}
}
}
/**
* Validates if placing a queen on the board at the given position is safe.
*
* @param board The current board state.
* @param x The row index.
* @param y The column index.
* @return True if safe, otherwise false.
*/
private boolean validate(char[][] board, int x, int y) {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < y; j++) {
// Check if there's any other queen in the same row, or diagonals
if (board[i][j] == 'Q' && (x + j == y + i || x + y == i + j || x == i)) {
return false;
}
}
}
return true;
}
/**
* Converts the board state to a list of strings.
*
* @param board The current board state.
* @return A list representing the board state.
*/
private List<String> construct(char[][] board) {
List<String> res = new ArrayList<>();
for (int i = 0; i < board.length; i++) {
res.add(new String(board[i]));
}
return res;
}
public static void main(String[] args) {
N_Queens nQueens = new N_Queens();
int n = 4;
List<List<String>> solutions = nQueens.solveNQueens(n);
// Print the solutions
for (List<String> solution : solutions) {
for (String row : solution) {
System.out.println(row);
}
System.out.println();
}
}
}