↑ ↓ to move · enter to open

000 · Combinatorics · Backtracking search

n-Queens

Place N queens on an N×N board so that no two share a row, a column, or a diagonal. Watch a backtracking search try every square, catch each attack, and back up when a row runs out of options.

Hover any square to see its row, column, and both diagonals.

How to read this: the search fills one row at a time, top to bottom (rows and columns are numbered from 0). Amber marks the square it is testing; if a red dashed line appears, an earlier queen attacks that square along that line.

  • square being tested
  • queen just placed
  • attacked: skip it
  • square already under attack (the Threats switch)
  • fading queen: picked back up (backtrack)
  • white queens: a complete solution

Try this: press Step a dozen times and watch a row run out of safe squares, then press Run.

Tracestep 0

Algorithm


          
  • step running now
  • the test just failed

Call stackdepth 0

Each line is one row the search is working on. The outlined one is the row it's on now; depth counts the rows.

Occupied lines3 lookups per test

colsc
diag ↘r − c
anti ↙r + c

How to read this: every column and every diagonal on the board gets one box. A box turns green once a queen sits on that line, so testing a square only means looking up three boxes. Each diagonal is named by one number: row minus column (r − c) for ↘ lines, row plus column (r + c) for ↙ lines, which stays the same all along the line.

  • line already has a queen
  • line being checked now
  • checked line is taken: conflict

Try this: hover a square on the board and see which three boxes light up.

0
Squares tried
0
Placed
0
Backtracks
0
Solutions

solutions found

Each solution the search finds lands here. Select one to inspect it on the board.

why it works

Three ideas turn an astronomically large search into one a laptop finishes in milliseconds.

i.One queen per row

Two queens in the same row attack each other, so a valid board has exactly one queen in every row. That lets the search work row by row and only choose a column each time.

choices: C(N², N) → NN → N!

Adding "one per column" turns each board into a permutation of the columns, cutting the space to N!.

ii.Diagonals are constant sums

Every square on a ↘ diagonal has the same value of r − c. Every square on a ↙ diagonal has the same r + c. So a diagonal is just one integer.

attacked(r,c) ⇔ c∈cols ∨ r−c∈diag ∨ r+c∈anti

Keeping three sets of occupied lines makes the attack test constant time. Hover the board to see the values.

iii.Prune early, back up

Backtracking abandons a partial board the moment it breaks a rule. One bad queen in row 2 eliminates every board that would have extended it, which is most of the tree.

explore → fail fast → undo → next column

The funnel below measures that pruning for the current N.

Search space for N = 8log scale

How to read this: each step down the funnel is a smarter way to count the boards worth checking. Bars use a log scale, so a bar twice as long as another means the number has twice as many digits.

  • boards left to check
  • white bar: boards that actually work
  • striped bar: still being counted

Solution counts by NOEIS A000170 · known to N = 27

  • row for the board size you picked
  • bar length: number of digits in the count
Live: counted in your browser. Pub: published results. N = 27 was finished in 2016 by the Q27 project at TU Dresden, using FPGAs for about a year. N = 28 has never been counted exactly; Monte Carlo estimates put it near 2.6 × 10¹⁸.