StaffBacktracking
N-Queens II
The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other.
Given an integer n, return the number of distinct solutions to the n-queens puzzle.
A queen can attack another queen if they share the same row, column, or diagonal.
Example 1
Input
n = 4Output
2There are exactly two distinct ways to place 4 queens on a 4 x 4 board so that none attack each other.
Example 2
Input
n = 1Output
1A single queen can be placed on the only square of a 1 x 1 board.
Constraints
- 1 <= n <= 9