Problem
Problem page: https://leetcode.com/problems/n-queens/
Topics: Backtracking
Difficulty: Hard
Similar problems: (H) N-Queens II
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.
Given an integer n, return all distinct solutions to the n-queens puzzle.
Each solution contains a distinct board configuration of the n-queens' placement, where
'Q'and'.'both indicate a queen and an empty space respectively.For example,
There exist two distinct solutions to the 4-queens puzzle:
[ [".Q..", // Solution 1 "...Q", "Q...", "..Q."],
["..Q.", // Solution 2 "Q...", "...Q", ".Q.."] ]
Analysis
In chess, a queen can move horizontally or vertically like a chariot in Chinese chess, and it can also move along either diagonal.
If two points lie on the same diagonal, the absolute difference between their x-coordinates equals the absolute difference between their y-coordinates.
Code
1 | class Solution: |
Article link: https://xnerv.wang/en/leetcode-n-queens/

