Places N queens on an N×N chessboard so no two queens threaten each other.
The N-Queens problem is a classic backtracking puzzle: place N chess queens on an N*N board so that no two queens can attack each other. Queens attack along rows, columns, and diagonals, so every queen must be on a unique row, column, and pair of diagonals. First proposed by chess composer Max Bezzel in 1848, the problem attracted the attention of luminaries including Carl Friedrich Gauss, who worked on enumerating all solutions for the 8-queen case. Over nearly two centuries, N-Queens has become one of the most studied problems in combinatorial optimization and remains a staple of computer science education.
Bezzel published the 8-Queens puzzle in a German chess newspaper, and within two years Franz Nauck published both the first complete solution set and the generalization to N queens on an N*N board. Gauss corresponded about the problem but never completed a full enumeration himself. In 1874, S. Gunther and J.W.L. Glaisher developed determinant-based approaches, foreshadowing the constraint satisfaction methods used today. The problem gained renewed importance in computer science when it became a benchmark for backtracking algorithms and, more recently, for SAT solvers and genetic algorithms.
An important optimization involves representing constraints with boolean arrays or bitmasks for rows, main diagonals, and anti-diagonals. This reduces the constraint check from O(N) to O(1) per placement attempt, significantly speeding up the search.
| Metric | Value |
|---|---|
| Solutions for N=8 | 92 |
| Unique solutions for N=8 | 12 (after removing rotations/reflections) |
| Time | O(N!) upper bound |
| Space | O(N) |
The branching factor decreases at each level due to constraints, so the actual runtime is much better than N! in practice. For the first column there are N choices, for the second roughly N-2, and so on. The number of solutions grows rapidly: N=10 has 724 solutions, N=12 has 14,200, and N=14 has 365,596.
Beyond basic backtracking, several techniques can accelerate the search. Constraint propagation eliminates impossible values for future columns as each queen is placed. Symmetry breaking exploits the fact that solutions related by rotation or reflection are equivalent, reducing the search space by up to a factor of 8. Bitwise operations represent the attacked positions as bitmasks, allowing extremely fast constraint checks on modern processors. These optimizations allow solvers to handle boards well beyond N=20.
The N-Queens problem is a foundational example of constraint satisfaction and backtracking. It appears in computer science education, interview questions, and as a benchmark for search algorithms. The techniques used -- constraint propagation, recursive backtracking, and pruning -- apply broadly to scheduling, resource allocation, VLSI design, and combinatorial optimization problems. Understanding N-Queens provides a gateway to harder problems such as graph coloring, Boolean satisfiability, and integer programming.