hard +40 pts

N-Queens Solutions

Count unique arrangements of n queens on an n×n chessboard with no two attacking each other.

In chess, a queen can attack any piece that lies in the same row, column, or diagonal. The N-Queens problem asks: how many ways can you place n queens on an n×n board so that no two queens attack each other? Implement a function `n_queens_count(n: int) -> int` that returns the total number of distinct valid configurations for a board of size `n`. Distinct configurations treat the board as fixed; rotations and reflections of the same arrangement count as different if they occupy different squares. For example, for n=4 there are exactly 2 valid placements.

Constraints

1 ≤ n ≤ 12 Your solution should be efficient enough to handle n=12 within a reasonable time (typically < a few seconds).

Example

>>> n_queens_count(1)
1
>>> n_queens_count(4)
2
>>> n_queens_count(8)
92
40 points ~35 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Place queens row by row, keeping track of which columns and diagonals are already occupied.
For an n×n board, each column index ranges 0..n-1, each main diagonal has constant (row - col), and each anti-diagonal has constant (row + col).
Use recursion with backtracking: try each column in the current row that is not threatened, place a queen, recurse to the next row, then undo the placement.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.