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.
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
Place one queen per row. At each row, try columns from left to right.
A candidate is safe if no earlier queen shares its column or either diagonal.
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.