All games

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

1a2b3c4d5e6f7g8h

15863724 means queens on a1, b5, c8, d6, e3, f7, g2 and h4.

Solve it by hand with knight’s moves

1a2b3c4d5e6f7g8h

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.

1a2b3c4d5e6f7g8h

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:

The 12 unique (fundamental) solutions

Turning or flipping a solution gives up to 8 versions. Here are the 8 of 15863724:

  1. As found15863724
  2. Turned 90°82417536
  3. Turned 180°57263148
  4. Turned 270°36428571
  5. Flipped left–right42736851
  6. Flipped top–bottom84136275
  7. Flipped on a1–h817582463
  8. Flipped on a8–h163571428

Solution 10 (35281746) is the same after a half turn, so it has 4 versions: 11 × 8 + 4 = 92.

  1. 1. 158637248 versions
  2. 2. 168374258 versions
  3. 3. 246831758 versions
  4. 4. 257138648 versions
  5. 5. 257418638 versions
  6. 6. 261748358 versions
  7. 7. 268314758 versions
  8. 8. 273685148 versions
  9. 9. 275814638 versions
  10. 10. 352817464 versions
  11. 11. 358417268 versions
  12. 12. 362581748 versions

All 92 solutions

In order of their numbers. The 12 unique solutions are in bold.

  1. 15863724
  2. 16837425
  3. 17468253
  4. 17582463
  5. 24683175
  6. 25713864
  7. 25741863
  8. 26174835
  9. 26831475
  10. 27368514
  11. 27581463
  12. 28613574
  13. 31758246
  14. 35281746
  15. 35286471
  16. 35714286
  17. 35841726
  18. 36258174
  19. 36271485
  20. 36275184
  21. 36418572
  22. 36428571
  23. 36814752
  24. 36815724
  25. 36824175
  26. 37285146
  27. 37286415
  28. 38471625
  29. 41582736
  30. 41586372
  31. 42586137
  32. 42736815
  33. 42736851
  34. 42751863
  35. 42857136
  36. 42861357
  37. 46152837
  38. 46827135
  39. 46831752
  40. 47185263
  41. 47382516
  42. 47526138
  43. 47531682
  44. 48136275
  45. 48157263
  46. 48531726
  47. 51468273
  48. 51842736
  49. 51863724
  50. 52468317
  51. 52473861
  52. 52617483
  53. 52814736
  54. 53168247
  55. 53172864
  56. 53847162
  57. 57138642
  58. 57142863
  59. 57248136
  60. 57263148
  61. 57263184
  62. 57413862
  63. 58413627
  64. 58417263
  65. 61528374
  66. 62713584
  67. 62714853
  68. 63175824
  69. 63184275
  70. 63185247
  71. 63571428
  72. 63581427
  73. 63724815
  74. 63728514
  75. 63741825
  76. 64158273
  77. 64285713
  78. 64713528
  79. 64718253
  80. 68241753
  81. 71386425
  82. 72418536
  83. 72631485
  84. 73168524
  85. 73825164
  86. 74258136
  87. 74286135
  88. 75316824
  89. 82417536
  90. 82531746
  91. 83162574
  92. 84136275

N-Queens: every board from 4×4 up has a solution

BoardSolutionsUnique
1×111
2×200
3×300
4×421
5×5102
6×641
7×7406
8×89212
9×935246
10×1072492
11×112,680341
12×1214,2001,787
13×1373,7129,233
14×14365,59645,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:

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)
Play the 8 Queens Puzzle (free)