Mathwords logoMathwords

Eight Queens Problem — Definition, Formula & Examples

The Eight Queens Problem is the challenge of placing eight chess queens on a standard 8×8 chessboard so that no two queens threaten each other — meaning no two share the same row, column, or diagonal.

The Eight Queens Problem asks for all arrangements of 8 non-attacking queens on an 8×88 \times 8 board, where two queens attack each other if they occupy the same row, column, or diagonal. It is a specific instance of the more general nn-queens problem on an n×nn \times n board and serves as a classic constraint-satisfaction problem in combinatorics and computer science.

How It Works

Since each row must contain exactly one queen, you can think of a solution as choosing one column for each row. Start by placing a queen in row 1, then try to place one in row 2 that doesn't conflict. If you reach a row where no safe column exists, backtrack and move the previous queen. This trial-and-error strategy is called backtracking. A computer can systematically explore all possibilities this way. For the 8×8 board, there are exactly 92 distinct solutions (or 12 if you count rotations and reflections as equivalent).

Worked Example

Problem: Verify that the following placement is valid for the first 4 rows: queen in row 1 at column 2, row 2 at column 4, row 3 at column 6, row 4 at column 8.
Step 1: Check columns: the queens occupy columns 2, 4, 6, and 8 — all distinct, so no column conflict.
Step 2: Check diagonals using the rule that two queens at positions (r1,c1)(r_1, c_1) and (r2,c2)(r_2, c_2) share a diagonal if r1r2=c1c2|r_1 - r_2| = |c_1 - c_2|. Compare every pair:
12=1,  24=2  13=2,  26=4  14=3,  28=6  |1-2|=1,\;|2-4|=2 \;\checkmark \quad |1-3|=2,\;|2-6|=4 \;\checkmark \quad |1-4|=3,\;|2-8|=6 \;\checkmark
Step 3: Continue checking the remaining pairs: (2,4)→(3,6): 12|1|\neq|2| ✓; (2,4)→(4,8): 24|2|\neq|4| ✓; (3,6)→(4,8): 12|1|\neq|2| ✓. No diagonal conflicts exist either.
Answer: The partial placement is valid — no two of these four queens attack each other. You would continue placing queens in rows 5 through 8 using the same checks.

Why It Matters

The Eight Queens Problem is a gateway to understanding backtracking algorithms, which appear throughout computer science courses and coding interviews. It also illustrates constraint-satisfaction problems that arise in scheduling, circuit design, and resource allocation.

Common Mistakes

Mistake: Forgetting to check diagonals and only verifying that no two queens share a row or column.
Correction: Queens also attack along diagonals. Always verify that r1r2c1c2|r_1 - r_2| \neq |c_1 - c_2| for every pair of queens.