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.
Example 1:
Input: n = 4
Output: 2
Explanation: There are two distinct solutions to the 4-queens puzzle as shown.
Example 2:
Input: n = 1
Output: 1
Constraints:
1 <= n <= 9
Solutions
Solution 1: Backtracking
We design a function \(dfs(i)\), which represents starting the search from the \(i\)th row, and the results of the search are added to the answer.
In the \(i\)th row, we enumerate each column of the \(i\)th row. If the current column does not conflict with the queens placed before, then we can place a queen, and then continue to search the next row, that is, call \(dfs(i + 1)\).
If a conflict occurs, then we skip the current column and continue to enumerate the next column.
To determine whether a conflict occurs, we need to use three arrays to record whether a queen has been placed in each column, each positive diagonal, and each negative diagonal, respectively.
Specifically, we use the \(cols\) array to record whether a queen has been placed in each column, the \(dg\) array to record whether a queen has been placed in each positive diagonal, and the \(udg\) array to record whether a queen has been placed in each negative diagonal.
The time complexity is \(O(n!)\), and the space complexity is \(O(n)\). Here, \(n\) is the number of queens.