Skip to main content

N-Queens

HardPremium

Place n queens on an n x n chessboard so that no two queens attack each other. A queen attacks every square in its row, its column and both of its diagonals.

Return every distinct arrangement. Write each board as a list of n strings, one per row, where "Q" marks a queen and "." marks an empty square. The boards can be in any order.

Example 1

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

A 4 x 4 board has exactly two arrangements, and each is the mirror image of the other.

Example 2

Input: n = 3 Output: []

No arrangement works on a 3 x 3 board.

Constraints

  • 1 <= n <= 9