hardBacktrackingPure DSA~35 min
Count Conflict-Free Queen Placements
Count the number of ways to place n non-attacking queens on an n x n board (no two share a row, column, or diagonal).
Problem
Given an integer n, return the number of distinct solutions to the n-queens puzzle: placements of n queens on an n x n chessboard so that no two attack each other.
Input
A single integer n.
Output
An integer: the number of distinct solutions.
Constraints
- 1 <= n <= 9
- Queens attack along rows, columns, and both diagonals.
- Only the count is required, not the boards.
Examples
Example 1
Input
n = 4
Output
2
Two distinct non-attacking placements exist.
Example 2
Input
n = 1
Output
1
A single queen on a 1x1 board.