Skip to content

N-Queens (return the boards)

HardBacktracking
Decode it first. Clues, constraints, pattern and plan before any code.

Place n queens on an n by n chessboard so that no two queens attack each other (no shared row, column or diagonal). Return every distinct board, in any order. Each board is a list of n strings, one per row, with "Q" for a queen and "." for an empty square.

n = 4  →  [[".Q..",
            "...Q",
            "Q...",
            "..Q."],
           ["..Q.",
            "Q...",
            "...Q",
            ".Q.."]]
 
n = 1  →  [["Q"]]

Constraints

  • 1 <= n <= 9