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. Input: totalNQueens 4Output: 2
  2. Input: totalNQueens 1Output: 1

Submitting also runs 8 hidden tests.

Constraints

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

Without the Pass you can run 3 Pass problems against their examples. Submitting, the hints and the editorial come with the Academy Pass. The daily problem and one Medium problem a week are 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