The N-Queens puzzle is the classic backtracking benchmark. Brute force over all placements is hopeless; backtracking with pruning (abandon a partial board as soon as two queens attack) makes it easy.
Write total_n_queens(n) that counts the ways to stand n queens on an n x n chessboard so that none of them attacks another: no two queens on the same row, column or diagonal. Boards that are rotations or reflections of one another still count separately; on a 4 x 4 board, for instance, there are two answers and each is the mirror image of the other.
Examples
Input: n = 6
Output: 4
Input: n = 5
Output: 10
Input: n = 3
Output: 0
Constraints
1 <= n <= 8- Target: backtracking with O(1) attack checks; n = 8 should take well under a second
Goals
- Place one queen per row and backtrack over the column choices
- Track attacked columns and diagonals with sets so each check is O(1)
- Prune the search early instead of validating complete boards