Back to DSA

N-Queens

hard
Acceptance: 44%
BacktrackingMatrix

Place n queens on an n-by-n chessboard so that no two queens threaten each other. Queens attack along rows, columns, and both diagonals. Return every distinct valid arrangement, where each arrangement is represented as a list of strings showing the board.

Examples

Example 1:
Input:n = 5
Output:10 solutions exist (e.g. ["Q....","..Q..","....Q",".Q...","...Q."] is one)
Example 2:
Input:n = 1
Output:[["Q"]]

Hints

00:00
1234567