-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathn_queens.java
112 lines (90 loc) · 4.82 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* Problem :- N-Queens
https://leetcode.com/problems/n-queens/description/
Approach:-
The approach uses backtracking to generate all possible configurations of queens on the board and checks the validity of each configuration.
It maintains a boolean board to represent the placement of queens, where `true` indicates the presence of a queen in a particular cell.
The algorithm starts by placing a queen in the first row and proceeds recursively to the next row, checking all possible column positions for the queen.
If a valid position is found, the algorithm moves to the next row and repeats the process. If all N queens are placed on the board,
a valid solution is found and added to the list of solutions.
To check the validity of a queen's position, the algorithm verifies three conditions:
1. Vertical Check: It checks the columns of the previous rows to ensure that there are no queens in the same column.
2. Left Diagonal Check: It checks the diagonal elements on the left side of the current position to ensure that there are no queens present.
3. Right Diagonal Check: It checks the diagonal elements on the right side of the current position to ensure that there are no queens present.
The algorithm continues this process, backtracking whenever it encounters an invalid position or explores all possibilities. Once all valid configurations are found, they are converted into a list of strings representing the board configurations, and the list of solutions is returned.
Time Complexity: O(N!)
In the worst case, the backtracking algorithm explores all possible configurations,. However, with pruning techniques, the actual runtime is significantly lower.
Space Complexity: O(N^2)
because the boolean board of size NxN is used to represent the placement of queens, and the list of solutions also occupies additional space.
*/
class Solution {
public List<List<String>> solveNQueens(int n) {
List<List<String>> list = new ArrayList<>();
// If the board size is 2 or 3, no solution is possible, so return an empty list
if (n == 2 || n == 3) {
return list;
}
// If the board size is 1, there is only one solution with a single queen in the only cell
if (n == 1) {
String ans = "Q";
ArrayList<String> a = new ArrayList<>();
a.add(ans);
list.add(a);
return list;
}
boolean arr[][] = new boolean[n][n]; // Create a boolean board to represent the placement of queens
queens(arr, 0, list); // Solve the N-queens problem recursively
return list; // Return the list of solutions
}
// Recursive function to solve the N-queens problem
static void queens(boolean board[][], int row, List<List<String>> list) {
if (row == board.length) {
ArrayList<String> arr = new ArrayList<>();
// Convert the boolean board to a list of strings representing the board configuration
for (int j = 0; j < board.length; j++) {
String ans = "";
for (int i = 0; i < board[j].length; i++) {
if (board[j][i] == false) {
ans += ".";
} else {
ans += "Q";
}
}
arr.add(ans);
}
list.add(arr); // Add the board configuration to the list of solutions
return;
}
for (int col = 0; col < board.length; col++) {
if (isSafe(board, row, col)) {
board[row][col] = true; // Place a queen in the current position
queens(board, row + 1, list); // Recursively solve the problem for the next row
board[row][col] = false; // Backtrack and remove the queen from the current position
}
}
}
// Function to check if it is safe to place a queen in a given position
private static boolean isSafe(boolean[][] board, int row, int col) {
// Check the vertical column for any previously placed queens
for (int i = 1; i <= row; i++) {
if (board[row - i][col]) {
return false;
}
}
// Check the left diagonal for any previously placed queens
int max = Math.min(row, col);
for (int i = 1; i <= max; i++) {
if (board[row - i][col - i]) {
return false;
}
}
// Check the right diagonal for any previously placed queens
int maxtimes = Math.min(row, board.length - col - 1);
for (int i = 1; i <= maxtimes; i++) {
if (board[row - i][col + i]) {
return false;
}
}
return true; // It is safe to place a queen in the given position
}
}