The n-Queens puzzle is a classic computer science problem that asks how to place n chess queens on an n×n chessboard such that no two queens threaten each other. In other words, no two queens can be on the same row, column, or diagonal.
In this blog, I will walk you through the code to solve the n-Queens problem using Java.
The Code Structure
The provided code consists of a Queen's
class with a few vital methods:
main(String[] args)
: the entry point of the program.solve(int i)
: the recursive function that attempts to place a queen in the correct position.promising(int i)
: checks if a particular coordinate is safe for a queen.
Understanding the Code
The Global Variables
private int n = 4;
private int[] col = new int[n + 1];
n
: represents the size of the board and the number of queens. In this example, it is set to 4, meaning it is a 4x4 board with four queens.col
: an integer array that represents the column positions of the queens for each row. The index of this array represents the row number, while the value at that index represents the column number. The array size isn+1
to simplify indexing starting from 1.
The solve
Method
public void solve(int i) {
int j;
if (promising(i)) {
if (i == n) {
for (int k = 1; k <= n; k++) {
System.out.println("(" + k + ", " + col[k] + ")");
}
} else {
for (j = 1; j <= n; j++) {
col[i + 1] = j;
solve(i + 1);
}
}
}
}
The solve
method is a recursive function that tries to find a solution for placing a queen in a specific row i
.
If the position is
promising
, it checks whether all the queens are placed (i.e.,i == n
). If so, it prints the solution.If not all queens are placed on the board, the algorithm will position a queen in the next row by recursively calling itself.
The promising
Method
public boolean promising(int i) {
int k = 1;
boolean promising = true;
while (k < i && promising) {
if (col[i] == col[k] || Math.abs(col[i] - col[k]) == i - k) {
promising = false;
}
k++;
}
return promising;
}
The promising
method checks whether it is safe to place a queen in the current position using the following conditions:
No two queens are in the same column:
col[i] == col[k]
.No two queens are in the same diagonal:
Math.abs(col[i] - col[k]) == i - k
.
How It Works
When the program runs, it begins by trying to place the first queen in the first row. For each row, it tries all possible column positions to find a safe spot, which is determined by the promising
method.
Once a safe spot is found for a queen, the algorithm attempts to place the next queen in the next row. This continues until all queens are placed or until it finds that it is not possible to place a queen in the current row without threatening another queen.
If a queen can not be placed in any column of the current row without threatening another queen, the algorithm backtracks, which means it goes back to the previous row and tries a new column position for that queen.
The process repeats until a solution is found or all possibilities are exhausted.
Conclusion
The n-Queens problem is a classic example of a combinatorial and backtracking problem. The provided Java code offers a simple and efficient solution by recursively trying all possibilities and pruning branches of the search tree that do not lead to a solution. With this approach, you can easily extend the code to solve the problem for larger board sizes by simply changing the value of n
.