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.
1 | Example 1: |
Solution
This is a backtracking problem. We can use a vector to store the position of the queens. The index of the vector represents the row and the value represents the column. We can use a function to check if the current position is valid. If it is valid, we can add it to the vector and call the function recursively. If the size of the vector is equal to n, we can add it to the result. Otherwise, we can remove the last element of the vector and try another position.
1 | class Solution { |
Variable explanation
In the N-Queens solution, the board
array and the row
and col
variables in the recursion have specific meanings that are crucial to understanding how the algorithm works. Let’s clarify these:
The
board
Array:- The
board
array is a 1D vector of sizen
, wheren
is the size of the chessboard (n x n). - Each element of the
board
array represents a row on the chessboard. - The value stored at each index of the
board
array represents the column position where a queen is placed in that row. - For example, if
board[2] = 3
, it means that in row 2, a queen is placed in column 3.
- The
The
row
Variable:- The
row
variable in thesolveNQueens
function represents the current row on the chessboard where you are trying to place a queen. - It is used to iterate through the rows of the chessboard, starting from 0 up to
n-1
.
- The
The
col
Variable:- The
col
variable in thesolveNQueens
function represents the column position in the current row where you are attempting to place a queen. - For each row, you try placing a queen in each column (from 0 to
n-1
) and check if it’s a safe placement.
- The
The
isSafe
Function:- This function checks whether placing a queen at the
board[row] = col
position is safe. - It checks for conflicts with queens in previous rows. There are two types of conflicts:
- Column conflict: If any previous row (
i
) has a queen in the same column (board[i] == col
). - Diagonal conflict: If any previous row (
i
) has a queen positioned diagonally. This is checked by the conditionabs(board[i] - col) == abs(i - row)
.
- Column conflict: If any previous row (
- This function checks whether placing a queen at the
Backtracking and Recursion:
- The
solveNQueens
function uses recursion to try placing queens row by row. - If a safe position is found for a queen in the current row, it moves to the next row.
- If no safe position is found in a row, it backtracks (i.e., it goes back to the previous row and tries a different column for the queen in that row).
- The
In summary, the board
array is a compact way to represent the positions of queens on the chessboard, and the row
and col
variables are used to navigate through the chessboard to place the queens safely. The algorithm explores all possible ways to place the queens, backtracking when necessary, until all solutions are found.