N-Queens
Place n queens on an n x n chessboard so that no two queens threaten each other. No two queens can share the same row, column, or diagonal. Return all distinct valid board configurations.
Each solution is represented as an array of strings where 'Q' marks a queen and '.' marks an empty square.
Examples
Input: n = 4
Output: [
[".Q..", "...Q", "Q...", "..Q."],
["..Q.", "Q...", "...Q", ".Q.."]
]
Input: n = 1
Output: [["Q"]]
Constraints
1 <= n <= 9
Sample Input
n = 4
Sample Output
[[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]
Constraints
- 1 <= n <= 9
Test Cases
Case 1
Args: [4]
Expected: [[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]
Topics
Place queens one row at a time using backtracking. Track which columns and diagonals are already attacked using sets. A queen at (row, col) occupies the row - col diagonal and the row + col anti-diagonal.
function solveNQueens(n) {
const result = [];
const cols = new Set();
const diag = new Set();
const antiDiag = new Set();
const board = Array.from({ length: n }, () => '.'.repeat(n));
function backtrack(row) {
if (row === n) {
result.push([...board]);
return;
}
for (let col = 0; col < n; col++) {
if (cols.has(col) || diag.has(row - col) || antiDiag.has(row + col)) continue;
cols.add(col);
diag.add(row - col);
antiDiag.add(row + col);
board[row] = board[row].substring(0, col) + 'Q' + board[row].substring(col + 1);
backtrack(row + 1);
cols.delete(col);
diag.delete(row - col);
antiDiag.delete(row + col);
board[row] = '.'.repeat(n);
}
}
backtrack(0);
return result;
}
Time: O(n!) — pruning eliminates most branches, but worst case is factorial. Space: O(n) for the sets and recursion stack.
Saved in this browser only. Private to you.