8 Queens Problem: Solutions and How to Solve It
There are 92 ways to place 8 queens with no attacks. See them all, and how to do it by hand.
Play the 8 Queens Puzzle (free)The answer: 92 solutions, 12 unique
15863724 means queens on a1, b5, c8, d6, e3, f7, g2 and h4.
Solve it by hand with knight’s moves
- a2, b4, c6, d8: knight’s moves (one column right, two rows up)
- e3, f1, g7, h5: the other four
That gives 24683175, one of the 12 unique solutions.
Backtracking: when a column is stuck, step back
Column by column, use the lowest safe square. If none is left, move the previous queen up.
Start in column a. Each column gets one queen, tried from the bottom square up.
Placed 0 · Solutions 0 / 92
Striped: attacked. Yellow: the column being tried. Blue: the queen just placed or moved.
Backtracking finds all 92 in 2,056 placements
The first solution (15863724) comes after 113. Trying every position would mean far more:
- Any 8 of the 64 squares: 4,426,165,368 ways
- One per column: 16,777,216 ways
- One per column and per row: 40,320 ways
The 12 unique (fundamental) solutions
Turning or flipping a solution gives up to 8 versions. Here are the 8 of 15863724:
- As found15863724
- Turned 90°82417536
- Turned 180°57263148
- Turned 270°36428571
- Flipped left–right42736851
- Flipped top–bottom84136275
- Flipped on a1–h817582463
- Flipped on a8–h163571428
Solution 10 (35281746) is the same after a half turn, so it has 4 versions: 11 × 8 + 4 = 92.
- 1. 158637248 versions
- 2. 168374258 versions
- 3. 246831758 versions
- 4. 257138648 versions
- 5. 257418638 versions
- 6. 261748358 versions
- 7. 268314758 versions
- 8. 273685148 versions
- 9. 275814638 versions
- 10. 352817464 versions
- 11. 358417268 versions
- 12. 362581748 versions
All 92 solutions
In order of their numbers. The 12 unique solutions are in bold.
- 15863724
- 16837425
- 17468253
- 17582463
- 24683175
- 25713864
- 25741863
- 26174835
- 26831475
- 27368514
- 27581463
- 28613574
- 31758246
- 35281746
- 35286471
- 35714286
- 35841726
- 36258174
- 36271485
- 36275184
- 36418572
- 36428571
- 36814752
- 36815724
- 36824175
- 37285146
- 37286415
- 38471625
- 41582736
- 41586372
- 42586137
- 42736815
- 42736851
- 42751863
- 42857136
- 42861357
- 46152837
- 46827135
- 46831752
- 47185263
- 47382516
- 47526138
- 47531682
- 48136275
- 48157263
- 48531726
- 51468273
- 51842736
- 51863724
- 52468317
- 52473861
- 52617483
- 52814736
- 53168247
- 53172864
- 53847162
- 57138642
- 57142863
- 57248136
- 57263148
- 57263184
- 57413862
- 58413627
- 58417263
- 61528374
- 62713584
- 62714853
- 63175824
- 63184275
- 63185247
- 63571428
- 63581427
- 63724815
- 63728514
- 63741825
- 64158273
- 64285713
- 64713528
- 64718253
- 68241753
- 71386425
- 72418536
- 72631485
- 73168524
- 73825164
- 74258136
- 74286135
- 75316824
- 82417536
- 82531746
- 83162574
- 84136275
N-Queens: every board from 4×4 up has a solution
| Board | Solutions | Unique |
|---|---|---|
| 1×1 | 1 | 1 |
| 2×2 | 0 | 0 |
| 3×3 | 0 | 0 |
| 4×4 | 2 | 1 |
| 5×5 | 10 | 2 |
| 6×6 | 4 | 1 |
| 7×7 | 40 | 6 |
| 8×8 | 92 | 12 |
| 9×9 | 352 | 46 |
| 10×10 | 724 | 92 |
| 11×11 | 2,680 | 341 |
| 12×12 | 14,200 | 1,787 |
| 13×13 | 73,712 | 9,233 |
| 14×14 | 365,596 | 45,752 |
Any board by hand: even rows, then odd rows
For columns a, b, c…, use rows 2, 4, 6, … and then 1, 3, 5, … Two kinds of size need a change:
- 8, 14, 20…: swap 1 and 3, then put 5 last (8×8: 2, 4, 6, 8, 3, 1, 7, 5)
- 9, 15, 21…: 2 after the evens, 1 and 3 last (9×9: 4, 6, 8, 2, 5, 7, 9, 1, 3)
N-Queens code: backtracking in Python and JavaScript
Python, the same search as above. Each solution lists a row per column (0 = bottom):
def solve_n_queens(n):
solutions, rows = [], []
def place(col):
if col == n: # all n queens placed
solutions.append(rows[:])
return
for row in range(n):
# safe if no earlier queen shares the row or a diagonal
if all(r != row and abs(r - row) != col - c
for c, r in enumerate(rows)):
rows.append(row)
place(col + 1)
rows.pop() # backtrack
place(0)
return solutions
print(len(solve_n_queens(8))) # 92
A faster JavaScript count, with the attacked rows and diagonals kept as bits:
function countQueens(n) {
const all = (1 << n) - 1;
let count = 0;
(function place(rows, up, down) {
if (rows === all) return void count++;
let free = all & ~(rows | up | down);
while (free) {
const bit = free & -free; // lowest free row
free ^= bit;
place(rows | bit, ((up | bit) << 1) & all, (down | bit) >> 1);
}
})(0, 0, 0);
return count;
}
countQueens(8); // 92
FAQ
- How many solutions does the 8 queens problem have?
- 92, or 12 if you count rotations and mirror images only once
- What is the first solution?
- 15863724: the smallest number, and the first one backtracking finds
- Can you put 9 queens on a chessboard without attacks?
- No. Each queen needs its own column, and there are only 8
- Is there a solution with a queen in a corner?
- Yes. 16 of the 92 do, such as 15863724 with its queen on a1
- Does the pattern work on every board?
- Yes, on every board from 4×4 up. We also checked it by computer from 4×4 to 30×30
- How many solutions do bigger boards have?
- Counted only up to 27×27, which has 234,907,967,154,122,528 (Q27 Project, 2016)
- Is the N-Queens problem hard for a computer?
- Finding one is easy, but all is slow: the count grows roughly like (0.143 n)ⁿ (Simkin, 2021)