Objective
Create a Python program that solves the classic N queens problem: place n queens on an n × n board so that none attack each other (not on the same row, the same column or a diagonal).
Instructions
- Read an integer
nbetween 1 and 8. - Place the queens row by row: in row
rowtry the columns from 1 tonin order. A column is safe if no previous queen is in that column or on a diagonal (same difference in columns as in rows). If it is safe, place it and go on; when coming back, remove it (backtracking). - Each solution is the list of the queens' columns, one per row. Show
Solutions: twith the total number of solutions andFirst solution: c1 c2 ... cnwith the first one you find, orNo solutionif there are none.
4
Solutions: 2
First solution: 2 4 1 3
Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.
Start exercise