Problem 577818 · hard · Level 05 Advanced Algorithms & Graphs

Every Peaceful Queen Layout

backtracking · constraint satisfaction · sets

A chess club wants posters showing every way to put n queens on an n x n board so that no two queens share a row, a column, or a diagonal. Return all such boards. Each board is a list of n strings, one per row from top to bottom, using "Q" for a queen and "." for an empty square. The boards may be returned in any order.

Examples

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

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

Input:  n = 3
Output: []

Constraints

  • 1 <= n <= 8

Goals

  • Place one piece per row and track attacked columns and diagonals in sets
  • Convert each placement into a printable board
Starting Python…