Skip to content

Eight queens

puzzles

Level 2 of 3

Also called The eight queens puzzle and N queens, by backtracking.

test yourself

Six questions, four to pick from and two for paper.

fresh numbers: the questions a run answers, asked again on new numbers

choose one

question 1

How many squares does the method check on a 4 by 4 board?

the answer, and why

the answer is A

a1 leads nowhere, so the method goes back four times before b1, d2, a3 and c4 fill the board. Section 5 counts all 26.

if you chose another

  • That is every square once. After going back, the method checks some squares again.
  • That is one square a row. Rows meet attacked squares first, as row 2 does under a1 with a2 and b2, and going back checks some again.
  • That is 4!4!, 4 times 3 times 2 times 1, the boards with one queen a row and a column.

question 2

How many times does the method put a queen on a square on 6 by 6? A queen moved along its row counts again.

the answer, and why

the answer is B

The method goes back many times on 6 by 6, and every queen it puts on a square counts, moves included. Section 3's table: 31.

if you chose another

  • That is the queens in the answer, with no going back.
  • That is one queen on every square.
  • That is 6!6!, 6 times 5 and on down to 1, the boards with one queen a row and a column.

question 3

How many boards have one queen in each row and each column, on 5 by 5?

the answer, and why

the answer is D

5 choices in row 1, then 4, 3, 2 and 1. 5 times 4 times 3 times 2 times 1 is 120.

this tests where the formula comes from, on the learn page

if you chose another

  • That is the squares on the board.
  • That is 5 to the power 5, which lets two queens share a column.
  • That is the answers, boards with no two queens on a diagonal too.

question 4

7 by 7 checks 42 squares, and 6 by 6 checks 171. Why can the bigger board cost less?

the answer, and why

the answer is C

The cost depends on how often the method goes back. On 7 by 7 it goes back twice; on 6 by 6, 25 times. Section 3's table shows it: 9 queens put down on 7 by 7 to end with 7, 31 on 6 by 6 to end with 6.

this tests best, worst and average, on the learn page

if you chose another

  • Every board has diagonals, and the method checks them.
  • 7 by 7 has 49 squares, and 6 by 6 has 36.
  • The method fills every row, one after another.

pen and paper

question 5about 6 minutes

Run the method by hand on a 5 by 5 board.

  1. Write the square each queen goes on, row by row.
  2. How many squares does each row check?
  3. Add them up.
a hint

In each row, check the squares from the left, and stop at the first one no queen above attacks.

a worked answer

a1, c2, e3, b4 and d5, and the method never goes back. Row 1 checks 1 square, row 2 checks 3, row 3 checks 5, row 4 checks 2 and row 5 checks 4. That is 15 squares.

this tests how it works, on the learn page

what a full answer includes
  • a1, c2, e3, b4, d5.
  • 1, 3, 5, 2 and 4.
  • 15 squares.

question 6about 6 minutes

A friend wants to solve 8 by 8 by trying every board with one queen in each row and each column, then looking at its diagonals.

  1. How many boards is that?
  2. How many squares does the method check instead?
  3. Why does the method need so few?
a hint

Use question 2 of the learn page, and section 3's table.

a worked answer

8 choices in row 1, then 7, and on: 8 times 7 times 6 times 5 times 4 times 3 times 2 times 1 is 40,320 boards. The method checks 876 squares. As soon as one queen is attacked, it moves on, so it never looks at the boards that start that way.

Question 2: trying every board instead, how many boards, BB, have one queen in each row and each column?
  1. the move: row 1

    the line:
    row 1’s queen hasn squares to choose\begin{gathered} \text{row 1's queen has} \\ n \text{ squares to choose} \end{gathered}

    why it is allowed: No queen is down yet, so any of its nn squares will do. For 4 by 4, a1, b1, c1 or d1.

  2. the move: the rows below

    the line:
    each row below has onecolumn fewer to choose\begin{gathered} \text{each row below has one} \\ \text{column fewer to choose} \end{gathered}
    row 1row 2row 3
    a1b2c3
    a1c2b3
    b1a2c3
    b1c2a3
    c1a2b3
    c1b2a3
    3 by 3, diagonals not looked at yet: 3 choices in row 1, each with 2 in row 2, each with 1 in row 3. 3 times 2 times 1, which is 6 boards.

    why it is allowed: Each queen above takes a column, and no two queens share one. So row 2 has one choice fewer than row 1, row 3 two fewer, and on down to 1.

  3. the move: multiply them

    the line:
    B=n!B = n!

    why it is allowed: By lines 1 and 2. Whichever square row 1 takes, row 2 has one fewer left, and whichever pair, row 3 two fewer, so the choices multiply, down to 1: n!n!. For 8 by 8, 40,320 boards, before any diagonal is looked at. The method checks 876 squares there.

try it with a small nn: at n=4n = 4, line 3 gives B=24B = 24, which you can work out by hand

this tests where the formula comes from, on the learn page

what a full answer includes
  • 40,320 boards.
  • 876 squares.
  • It stops a board at its first attacked queen, so every board that starts that way is skipped.