RECR-P5

Queens on the yard

Practice 5 of 6Recursion and backtracking6 tests

The problem

Return the number of ways to place n queens on an n × n board so that no two share a row, a column or a diagonal. Lesson 5 explains the rules and the search.

n_queens(4)    # 2
n_queens(8)    # 92
n_queens(3)    # 0

Limits

  • n is from 1 to 10.

Builds on RECR-5 Pruning.

Continue on a laptop

Practice runs Python in the browser and needs a keyboard and a wider screen. Read the problem and open hints here, then write your solution on a laptop. Progress is saved per browser for now, so hints opened here stay on this device.

Need a hint?0 of 3 opened

  1. Hint 1Nudge
  2. Hint 2Approach Open hint 1 first
  3. Hint 3Pseudocode Open hint 2 first
  4. Worked solutionlast resort Opens after all 3 hints
Python waits for a wider screensolution.py

Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.

6 tests, not run yet
Test results
#TestResultReads · time
14 by 4Not run
21 by 1Not run
32 and 3Not run
46 by 6Not run
58 by 8Not run
610 by 10Not run