On this page
A queen attacks along her row, her column and both diagonals. The puzzle is a clean way to learn how a careful search works, and every step can be checked with a pencil.
What is the eight queens puzzle?
Chess composer Max Bezzel published the puzzle in 1848, and Franz Nauck published the first solutions in 1850 and extended it to n queens on an n by n board, according to the Wikipedia history of the eight queens puzzle. In 1972 Edsger Dijkstra used it to illustrate structured programming with a depth-first backtracking algorithm (same source).
The eight queens puzzle has exactly 92 solutions (Wikipedia; OEIS A000170). If two solutions that differ only by a rotation or reflection of the board count as one, there are 12 fundamental solutions (OEIS A002562). Eleven of the 12 have eight variants each; one matches its own half-turn and has four, so 11 × 8 + 4 = 92 (Wikipedia, above).
How many ways can you place 8 queens, and how do you cut the search?
Choosing any 8 of the 64 squares gives 4,426,165,368 arrangements (Wikipedia). Two observations shrink this fast:
One queen per row leaves 8^8 = 16,777,216 choices.
One queen per column too makes the columns, read row by row, a permutation of 1 to 8: 8! = 40,320 candidates, and only diagonals left to check.
The diagonal test is one line: queens in rows i and j, columns p and q, attack diagonally exactly when |p − q| = |i − j|. If permutations are new to you, our guide to permutations and combinations Class 11 formulas covers the counting.
How does backtracking solve the 4-queens problem?
Backtracking places one queen per row, tries columns left to right, and steps back the moment a row has no safe square. Here is the full search on a 4 by 4 board, writing a position as the columns used in rows 1 to 4.
Row 1, column 1. Row 2 can only use column 3 or 4.
Row 2, column 3. Row 3 has columns 1 and 3 taken and columns 2 and 4 on a diagonal. Dead end: back up.
Row 2, column 4. Row 3 must be column 2, then row 4 needs column 3, diagonal to it. Dead end: column 1 in row 1 fails.
Row 1, column 2. Rows 2, 3, 4 are forced to columns 4, 1, 3. All safe: 2, 4, 1, 3.
Columns 3 and 4 in row 1 mirror columns 2 and 1, giving 3, 1, 4, 2 and a dead end.
So the 4-queens problem has exactly 2 solutions, matching OEIS A000170.
How many solutions does the n-queens problem have?
The number of solutions for n = 1 to 10, from OEIS A000170:
n = 1: 1
n = 2 and n = 3: 0
n = 4: 2
n = 5: 10
n = 6: 4
n = 7: 40
n = 8: 92
n = 9: 352
n = 10: 724
Notice that the count does not always grow: the 6 by 6 board has fewer solutions than the 5 by 5.
Why are n = 2 and n = 3 impossible?
For n = 2, any two squares of a 2 by 2 board share a row, a column or a diagonal, so a second queen is always attacked.
For n = 3, look at the middle row. A centre queen attacks all eight other squares. A queen at either end of the middle row leaves the top and bottom queens only the far column, where they attack each other.
Is there a formula that solves n queens for every n of 4 or more?
Yes. Explicit constructions for every n of 4 or more were published by Hoffman, Loessi and Moore in Mathematics Magazine (1969) and by Bernhardsson in the ACM SIGART Bulletin (1991). A simple version, listed on Wikipedia, gives the column of the queen in each row:
If n divided by 6 leaves remainder other than 2 or 3, list all even numbers up to n, then all odd numbers.
If the remainder is 2, swap 1 and 3 in the odd list and move 5 to the end.
If the remainder is 3, move 2 to the end of the even list and move 1, 3 to the end of the odd list.
For n = 8 (remainder 2) this gives 2, 4, 6, 8, 3, 1, 7, 5. Check it: the differences between column and row, 1, 2, 3, 4, −2, −5, 0, −3, are all different, and so are the sums, 3, 6, 9, 12, 8, 7, 14, 13. Distinct differences and sums mean no shared diagonal.
For another board puzzle settled by a permutation argument, see why the 15 puzzle with 14 and 15 swapped cannot be solved. To work through more problems with a step-by-step solver that explains the reasoning rather than printing an answer, try the Fermi Problem Library; for the ideas behind them, the Fermi Concept Library gives each concept its own lesson.



