🏠 Home

πŸ‘‘ The 8 Queens Problem

Place 8 queens on a chessboard so that none can attack another

/ 22
πŸ‘‘ The Challenge
Topics: combinatorics, backtracking, constraints, systematic search
A chess queen can attack any square in the same row, column, or diagonal β€” as far as it likes.

Challenge: Place 8 queens on an 8Γ—8 chessboard so that no two queens attack each other.

Every row and every column must contain exactly one queen. How many solutions exist?

Interactive board β€” click to place queens (click again to remove):

Click squares to place queens
For n queens on an nΓ—n board, each queen must be in a different row AND different column.

This means the column positions of queens form a permutation of {1, 2, …, n}.

Additionally, no two queens can share a diagonal: |row₁ βˆ’ rowβ‚‚| β‰  |col₁ βˆ’ colβ‚‚|.

Brute force: 8! = 40,320 permutations to check. With diagonal pruning, the real number of solutions is just 92.
Board size nSolutions
1Γ—11
2Γ—20
3Γ—30
4Γ—42
5Γ—510
6Γ—64
7Γ—740
8Γ—892
πŸ“ Questions
1
A queen is placed at row 3, column 5. List all the constraints imposed: which rows, columns, and diagonals are now blocked? 3 marks
2
On a 4Γ—4 board, one solution has queens at columns (2, 4, 1, 3) for rows 1–4. Verify this is valid by checking no two queens share a row, column, or diagonal. 3 marks
3
Why must each row and each column contain exactly one queen in a valid solution? 2 marks
4
If queen positions are represented as a permutation of columns, how many permutations of {1,2,3,4,5,6,7,8} exist? This is the maximum number of arrangements to check before applying diagonal constraints. 2 marks
5
The 8Γ—8 board has 92 solutions. But many are rotations or reflections of each other. How many are fundamentally distinct (not related by rotation/reflection)? 2 marks
6
Using the interactive board, find one valid solution for the 8Γ—8 board and write down the queen positions as a list (row: column). 4 marks
7
Extension: Describe a backtracking algorithm to solve the n-queens problem. How does pruning reduce the search space significantly? 6 marks

Answer Key

πŸ‘‘ 8 Queens Problem

1.Row 3 blocked entirely. Column 5 blocked entirely. Diagonals: all squares (r,c) where |rβˆ’3|=|cβˆ’5| are blocked β€” i.e. both main diagonals through (3,5).
2.Columns: 2,4,1,3 β€” all distinct βœ“. Diagonals: |1βˆ’2|=1β‰ |2βˆ’4|=2 βœ“; |1βˆ’3|=2β‰ |2βˆ’1|=1 βœ“; |1βˆ’4|=3β‰ |2βˆ’3|=1 βœ“; |2βˆ’3|=1β‰ |4βˆ’1|=3 βœ“; |2βˆ’4|=2β‰ |4βˆ’3|=1 βœ“; |3βˆ’4|=1β‰ |1βˆ’3|=2 βœ“. Valid solution.
3.A queen attacks its entire row and column. Placing two queens in the same row or column would make them attack each other immediately β€” so each must occupy a unique row and a unique column.
4.8! = 40,320 permutations.
5.12 fundamentally distinct solutions (92 Γ· 8 symmetries, accounting for reflections and rotations; one solution is symmetric so the exact count is 12).
6.Accept any valid 8Γ—8 arrangement. One example: rows 1–8 at columns 1,5,8,6,3,7,2,4.
7.Backtracking: place a queen in row 1, try each column. For row 2, try each column that doesn't conflict with row 1's queen (row, col, diagonal checks). Recurse. If no valid column exists, backtrack to previous row and try the next column. Pruning: reject any column immediately if it shares a column or diagonal with an already-placed queen, dramatically reducing branches explored.