The n-queens puzzle is the problem of placing n
queens on an n x n
chessboard such that no two queens attack each other.
Given an integer n
, return all distinct solutions to the n-queens puzzle. You may return the answer in any order.
Each solution contains a distinct board configuration of the n-queens' placement, and is represented as an array containing the column for each row of the matrix.
Example:
Input: n = 4 Output: [[1, 3, 0, 2], [2, 0, 3, 1]] Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above
Constraints:
1 <= n <= 9
The core challenge of the n-queens problem is to place n queens on an n x n chessboard such that no two queens can attack each other. This means no two queens can be in the same row, column, or diagonal.
This problem is significant in the field of computer science and mathematics as it involves backtracking, a fundamental algorithmic technique. It also has applications in constraint satisfaction problems and combinatorial optimization.
Potential pitfalls include misunderstanding the constraints of the problem, such as ensuring no two queens share the same diagonal, which can be tricky to implement correctly.
To solve the n-queens problem, we can use a backtracking approach. The idea is to place queens one by one in different columns, starting from the leftmost column. When placing a queen, we check for clashes with already placed queens. If a clash is found, we backtrack and try the next column.
Here is a step-by-step approach:
Here is a detailed breakdown of the algorithm:
/**
* Solves the n-queens problem and returns all distinct solutions.
* @param {number} n - The size of the chessboard.
* @return {number[][]} - A list of solutions, each solution is an array of column indices.
*/
function solveNQueens(n) {
const solutions = [];
const board = new Array(n).fill(0).map(() => new Array(n).fill('.'));
const cols = new Set();
const diag1 = new Set();
const diag2 = new Set();
function backtrack(row) {
if (row === n) {
const solution = board.map(r => r.join(''));
solutions.push(solution);
return;
}
for (let col = 0; col < n; col++) {
if (cols.has(col) || diag1.has(row - col) || diag2.has(row + col)) {
continue;
}
board[row][col] = 'Q';
cols.add(col);
diag1.add(row - col);
diag2.add(row + col);
backtrack(row + 1);
board[row][col] = '.';
cols.delete(col);
diag1.delete(row - col);
diag2.delete(row + col);
}
}
backtrack(0);
return solutions;
}
// Example usage:
console.log(solveNQueens(4));
The time complexity of the backtracking solution is O(N!), where N is the size of the chessboard. This is because we are trying every possible placement of queens and backtracking when necessary.
The space complexity is O(N) for the recursion stack and the sets used to track columns and diagonals.
Some potential edge cases include:
To handle these edge cases, we can add checks at the beginning of our function to return early for these values.
To test the solution comprehensively, we should include a variety of test cases:
We can use console logs or a testing framework like Jest to automate the testing process.
When approaching problems like the n-queens puzzle, it's important to:
The n-queens problem is a classic example of a backtracking problem. By understanding the constraints and using a systematic approach, we can find all possible solutions efficiently. This problem helps in developing a deeper understanding of backtracking and its applications in solving complex problems.
For further reading and practice, consider the following resources: