Problem 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. You may return the answer in any order.

Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space, respectively.

Examples

Example 1:

https://assets.leetcode.com/uploads/2020/11/13/queens.jpg

Input: n = 4
Output: [[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]
Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above

Example 2:

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

Constraints:

解析

給定一個正整數 n , 要把 n 個西洋棋的皇后放到 n by n 的棋盤使得 n 個皇后不會相互攻擊

根據西洋棋規則,西洋棋的皇后可以走正反對角線還有上下左右直線。或者說是米字形範圍

這個限制就是用來檢查是否能夠放皇后的規則

要簡化的這個問題, 可以利用上面那個規則

因為每個皇后水平方向會互斥,代表每個列只能放一個皇后

可以簡化成對 n 列, 選擇一個 col 位置給 皇后放