The N-Queens Problem places N non-attacking queens on an N x N chessboard using recursive backtracking constraints.
Place Queen 1 at (0, 0). Mark col 0, main diagonal, and anti-diagonal as attacked.
Row 0: Q at (0, 0) | Board: [Q, ., ., .]Try col 0 (blocked), col 1 (blocked), col 2 (safe). Place Q at (1, 2). Proceed to Row 2.
Row 1: Q at (1, 2) | Row 2: All columns blocked -> BACKTRACK!Adjust placements to find valid configuration: Q at (0, 1), (1, 3), (2, 0), (3, 2).
Valid Solution Found! Queens placed at: [(0,1), (1,3), (2,0), (3,2)]Place N guests at a table grid where no two guests are in the same row, column, or diagonal line.
250,000 steps (Quadratic)
O(N²) Quadratic Time
O(1) In-Place Space
Memory growth rate as N expands.
Relative efficiency rating for large N.
| Operation | Time Complexity | Space Complexity |
|---|---|---|
| Backtracking Solver | O(N!) | O(N) |
function solveNQueens(n) {
const res = [];
// Recursive placement with backtracking
return res;
}Powers Sudoku, Crossword, and VLSI chip placement engines.
Validate your conceptual understanding, operation mechanics, and core concepts of N Queens.
Use boolean lookup sets for `row+col` and `row-col` to perform O(1) diagonal collision checks during backtracking!
Board loaded. Select size and click Solve to watch recursive backtracking place queens.