Search
⌘K
Get Premium
Backtracking
N-Queens
hard
Count: 10
DESCRIPTION (inspired by Leetcode.com)
You're arranging a chess tournament and need to set up n queens on an n × n chessboard for a special demonstration. The challenge is to place all queens such that no two queens threaten each other.
In chess, a queen can attack any piece on the same row, column, or diagonal. Find all distinct arrangements where all n queens can coexist peacefully.
Constraints:
- 1 <= n <= 9
Example 1:
Input:
n = 4
Output:
[[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]
Explanation: There are two distinct arrangements for 4 queens on a 4×4 board where no queen attacks another.
Example 2:
Input:
n = 1
Output:
[["Q"]]