The N-Queens Problem is a combinatorial problem: we want to count (or find) arrangements of N non-attacking queens on an N×N board. Understanding the scale of the search space helps explain both why the problem is hard and why mathematical structure is essential.
The Naive Upper Bound: C(N², N)
The total number of ways to place N queens on N² squares (choosing N squares from N²) is:
C(N², N) = (N²)! / (N! × (N²-N)!)
For N = 8, this equals C(64, 8) = 4,426,165,368 — over 4 billion arrangements. Of these, only 92 are valid solutions.
The Row-Column Constraint: N!
Since every valid solution must have exactly one queen per row and exactly one queen per column, we can reformulate the problem: valid solutions correspond to permutations of {1, 2, ..., N} where the i-th element gives the column of the queen in row i.
This reduces the search space from C(N², N) to at most N! (permutations). For N = 8:
8! = 40,320
So we need to search through at most 40,320 permutations (rather than 4 billion+) to find all valid solutions. The diagonal constraints further reduce this to just 92 valid permutations — a reduction factor of over 400.
Computational Complexity Classification
The N-Queens Problem in its decision form (does a solution exist for an N×N board?) is actually trivial — a solution exists for all N ≥ 4. More interesting formulations include:
- Counting problem: How many solutions exist for N×N? This is a #P problem (counting problems that are computationally hard)
- Enumeration problem: List all solutions. Also computationally intensive for large N
- Optimization variants: Placing as many non-attacking queens as possible on boards with obstacles falls in NP-complete territory
Why the Diagonal Constraint is the Hard Part
Of the three types of conflicts (row, column, diagonal), diagonal conflicts are hardest to handle algorithmically because there are 2N − 1 diagonals in each direction (forward and backward), giving 4N − 2 diagonal constraints in total. For N = 8, that is 30 diagonal constraints to track simultaneously, compared to just 8 row constraints and 8 column constraints.
For an intuitive understanding, see our solving guide, which explains diagonal detection strategies accessible to beginners.