Algorithms

N-Queens Backtracking

The N-Queens problem asks for a placement of N queens with no shared row, column, or diagonal. Watch backtracking explore, reject, and finally solve a 6×6 board.

7 minLevel 2/3The interactive stage stays ad-free.
Step0 / 197Backtracks0StatusSearchingBoard6 × 6

Queens are placed one row at a time. Red cells are attacked by an existing queen; amber marks the attempt being considered, and rose marks a backtrack. The first complete board is one valid solution.

What you are seeing

1

Place one queen per row. At each row, try columns from left to right.

2

A candidate is safe if no earlier queen shares its column or either diagonal.

3

When no safe column remains, undo the previous choice and continue. The first complete board is a solution.

Why it matters

Backtracking is the foundation of constraint solving, SAT solvers, scheduling, and puzzle engines. N-Queens makes the search tree visible.

Complexity & details

Worst case: O(N!) placements without pruning. Conflict checks and pruning cut the search dramatically in practice.

Frequently asked questions

Why is one queen per row enough?+

N queens on an N×N board with no shared row must occupy every row exactly once. Placing row by row removes the row constraint from the search.

What is backtracking doing?+

It commits to a choice, explores its consequences, then undoes the choice if the branch cannot lead to a solution.