Module 9 · Backtracking · Backtracking: Inclusion–Exclusion

N-Queens

Every row gets exactly one queen, so placeQueen(row) only chooses a column. It tries each column in turn: when no queen above attacks the square (same column, or either diagonal) the queen goes there and the next row is called; when that call returns, the queen is lifted and the next column is tried.

A faded queen is the square being tried. If a queen above attacks it, a red line joins them. The call stack shows the open placeQueen calls, one per row with a queen; allBoards collects each full board.