12. N-Queens
Last updated
Was this helpful?
Last updated
Was this helpful?
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 all distinct solutions to the n-queens puzzle.
Each solution contains a distinct board configuration of the n-queens' placement, where 'Q'
and '.'
both indicate a queen and an empty space, respectively.
Example 1:
Example 2:
Approach: Try each possible cell and check if it is safe or not check for row, col, diagonal Diagonal Formula = row + col and row - col
Time Complexity: O(n!)