Count N-Queens solutions
Return the number of ways to place n nonattacking queens on an n by n board, one per row. Count rotations separately; n=0 has one empty solution.
occupied_columns, occupied_down = {1}, {-1}
row, col = 1, 2
print(col in occupied_columns or row-col in occupied_down)Reason about the state
Backtrack row by row while tracking occupied columns and both diagonals. Restore sets after each recursive choice to isolate sibling branches. Before writing code, identify what each variable means after one iteration. Walk through the example and explain why the next transition preserves that meaning. Then check the smallest permitted input and a case with repeated values. A correct answer follows the contract for every input, including the boundary cases in the checks. Keep any input mutation within the stated rules; return a fresh result when the task asks for one.
Your task
Return the number of ways to place n nonattacking queens on an n by n board, one per row. Count rotations separately; n=0 has one empty solution.
The explanation opens here once every check passes.