Backtracking · Hard
A constraint-search exercise places one queen in every row of an n by n board. No two queens may share a column or diagonal. The task is a specialist backtracking problem: board symmetries are not merged and no board rendering is required.
Count every valid full-board queen placement with no shared row, column or diagonal.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
{ n: integer }
Fields
- n is the number of rows and columns in the square board.
Output
Return value
- Return the number of placements, not boards or coordinates. Reflections and rotations count separately when they produce a different placement.
- The empty board n = 0 has one empty placement. Boards of size two and three have no solution.
Constraints
- 0 <= n <= 12.
Examples
Example 1
{
"n": 4
}Returns
2Example 2
{
"n": 2
}Returns
0Required complexity
O(n!) search states with conflict pruning and O(n) recursion space