Description

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 where 'Q' indicates a queen and '.' indicates an empty space.

Examples

Input:n = 4
Output:[[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]
Explanation:

Backtracking finds 2 solutions by placing queens row-by-row, checking column and diagonal conflicts.

Input:n = 1
Output:[["Q"]]
Explanation:

Trivial base case: one queen on a 1x1 board has exactly one valid placement.

Input:n = 2
Output:[]
Explanation:

On a 2x2 board, any queen placement leaves all remaining squares under attack. No solution exists.

Constraints

  • 1 ≤ n ≤ 9

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!