25. N-Queens

HardProgramAcademy Pass40 points

Place n queens on an n × n chessboard so that no two share a row, a column or a diagonal. Write totalNQueens n, the number of different placements.

The eight-queens puzzle has 92 solutions. The hidden tests go up to n = 9.

Examples

  1. InputtotalNQueens 4Output2
  2. InputtotalNQueens 1Output1

Submitting also runs 8 hidden tests.

Constraints

  • totalNQueens 0 = 1: the empty board is one placement.

Running and submitting solutions, the hints and the editorial come with the Academy Pass. The daily problem is open to every account.

Solution.lean
Loading editor…

Run checks your code against the examples and your custom inputs and records nothing. Submit also runs the hidden tests, confirms the axioms your proof uses and records the result. Each uses one compiler check.

ReadyLn 1, Col 1Lean 4
Draft saved in this browser