Place n chess queens on an n × n board so that no two attack each other. It is the standard example of backtracking.
The Problem
A queen attacks along its row, its column and both diagonals. The task is to place n queens so that no two share a row, a column or a diagonal. There are C(n², n) ways to place n queens on the board, about 4.4 billion for n = 8, but the constraints cut this drastically. Because each row holds exactly one queen, we can place queen i in row i and only choose its column x[i]. This reduces the search to n! permutations at most.
The Attack Test
Let queens be at (i, x[i]) and (j, x[j]), with rows i and j. They attack if they share a column, x[i] = x[j], or a diagonal, which happens exactly when |x[i] − x[j]| = |i − j|. The function place(k, c) checks column c for the k-th queen against all earlier queens in O(k) time. The two diagonal sets can also be tracked by marks on i + x and i − x, giving O(1) tests.
Backtracking Solution
Place queens row by row. For row k, try columns 1 to n; if column c is safe, set x[k] = c and go to row k + 1; if k = n a solution is found; if no column is safe in a row, backtrack to row k − 1 and move that queen to its next column. For n = 4 there are exactly 2 solutions, one the mirror image of the other: columns (2, 4, 1, 3) and (3, 1, 4, 2). For n = 1 there is 1 solution; n = 2 and n = 3 have none; n = 5 has 10; n = 6 has 4; n = 7 has 40; n = 8 has 92 solutions (12 distinct up to rotation and reflection).
Complexity
The state-space tree has 1 + n + n² + ... + nⁿ nodes if no pruning is used, and n! leaves if only column conflicts are removed. With backtracking, far fewer nodes are visited: for n = 8 the search visits about 2,000 nodes instead of millions. The worst-case time is still exponential, roughly O(n!), and the space is O(n) for the array x. The n-Queen problem also teaches symmetry: a solution yields others by reflection and rotation, which can halve the search.
Definition: n-Queen problem. The problem of placing n queens on an n × n chessboard so that no two queens lie in the same row, column or diagonal.
Table 4.2: Number of solutions of n-Queen
| n | Solutions | Remark |
|---|---|---|
| 4 | 2 | Mirror images |
| 5 | 10 | |
| 6 | 4 | Fewest after n = 4 |
| 8 | 92 | 12 distinct by symmetry |
Example 4.2: Four queens by backtracking
Problem: Place 4 queens on a 4 × 4 board.
Solution:
- Row 1: try column 1. Row 2: columns 1, 2 are attacked; column 3 is safe. Row 3: columns 1, 2, 3, 4 are all attacked → backtrack.
- Row 2: try column 4. Row 3: column 2 is safe. Row 4: columns 1–4 all attacked → backtrack; no more for row 3, so backtrack to row 1.
- Row 1: try column 2. Row 2: column 4. Row 3: column 1. Row 4: column 3 is safe.
- Solution: columns (2, 4, 1, 3), i.e. queens at (1,2), (2,4), (3,1), (4,3).
Result: First solution is (2, 4, 1, 3); the other is (3, 1, 4, 2).
def solve(n, row=0, cols=()):
if row == n:
return [cols]
out = []
for c in range(1, n + 1):
if all(c != x and abs(c - x) != row - i
for i, x in enumerate(cols)):
out += solve(n, row + 1, cols + (c,))
return out
print(solve(4))
for n in range(4, 9):
print("n =", n, "solutions =", len(solve(n)))
Output
[(2, 4, 1, 3), (3, 1, 4, 2)]
n = 4 solutions = 2
n = 5 solutions = 10
n = 6 solutions = 4
n = 7 solutions = 40
n = 8 solutions = 92
Common mistakes
- Checking only rows and columns and forgetting the diagonals.
- Testing the diagonal condition with the wrong formula; it is |x[i] − x[j]| = |i − j|.
- Placing two queens in a row, which loses the one-queen-per-row reduction.
- Believing every n has a solution; n = 2 and n = 3 do not.
Key takeaways
- One queen per row; choose the column of each; no shared column or diagonal.
- Attack test: x[i] = x[j] or |x[i] − x[j]| = |i − j|.
- n = 4 has 2 solutions; n = 8 has 92.
- Worst-case time is exponential, about O(n!), but pruning helps greatly.
Practice questions
- When do two queens at (i, x[i]) and (j, x[j]) attack on a diagonal?
- How many solutions does the 4-queen problem have?
- What is the space needed by the backtracking n-Queen algorithm?
Answers to the practice questions
- When |x[i] − x[j]| = |i − j|.
- Two, (2, 4, 1, 3) and (3, 1, 4, 2).
- O(n) for the array of column positions.
Sources
- Official BCA syllabus Design and Analysis of Algorithms, course code 26BCA404DS03, Unit IV
