Algorithms / Backtracking

Backtracking

Place four queens on a small chessboard by trying a square, checking the rules, and taking moves back when they lead nowhere. The same recipe solves many puzzles.

Intermediate7 steps~7 min
Queen — a choice that has been made
Cross — a square tested and found attacked
Dashed queen — a choice that was taken back
Solution so far — the choices currently standing
// THE SHORT VERSION

In 30 seconds

Backtracking builds a solution one choice at a time and checks the rules after every choice.

If a choice breaks a rule, or leaves no legal next move, it is undone and the next option is tried. When a step runs out of options, the search goes back one step further.

Because a bad partial solution is dropped at once, everything that would have been built on top of it is skipped. This pruning is why backtracking beats trying every combination, though in the worst case it can still be slow.

Backtracking, step by step

STEPS01 / 07

Queens go one per row, columns tried left to right; the counts are for reaching the first solution. “Full boards” means every way to put one queen in each row.

STEP 01 / 07 The puzzle
THE MATH
no two queens share a row, column or diagonal
  1. Step 1: The puzzle. Put four queens on a 4 × 4 board so that no queen attacks another. A queen attacks along its row, its column and both diagonals. A single queen near the middle already rules out 11 of the other 15 squares, so filling the board by guesswork rarely works.
  2. Step 2: Build it one row at a time. Backtracking builds a solution piece by piece and checks the rules after every piece. Each row needs exactly one queen, so go row by row and take the first safe square. Row 0 gets column 0. In row 1, columns 0 and 1 are attacked, so the queen goes to column 2.
  3. Step 3: Dead end. Row 2 has no safe square: every column is attacked by one of the two queens. The board is only half full, yet it can never be completed. So the search stops here instead of filling in the rest. Dropping a partial solution the moment it cannot work is the heart of backtracking.
  4. Step 4: Undo and retry. Step back one decision: lift the last queen and move it to its next untried square, column 3. Earlier choices stay as they are. From this new position row 2 does have a safe square, column 1, so the build carries on from there.
  5. Step 5: Back up further. Row 3 is blocked again. Undoing row 2 does not help, because its remaining squares are attacked, and row 1 has no columns left to try. When a row runs out of options the search steps back another level, here all the way to the first queen, which moves on to column 1.
  6. Step 6: A solution. With the first queen in column 1 the other rows fall into place: column 3, column 0, column 2. Every row has a queen and no rule is broken, so this is a solution. To list every solution, the search would simply undo and carry on; this board has two.
  7. Step 7: Pruning the search tree. Every choice is a branch in a tree. A dead end cuts off the whole subtree below it without visiting it: that is pruning. Here 26 squares were tested instead of checking 256 complete boards. Sudoku, mazes, crosswords and timetables are searched the same way: choose, check, undo.
VALUES SO FAR
board4 × 4, 4 queenspuzzle
row 0····choice
row 1····choice
row 2····dead end
row 1····retry
row 0····retry
columns····result
tested····cost
boards····skipped
// BEFORE

Learn this first

Nothing. This concept starts from scratch.

Spotted a mistake on this page?