N-Queens
HardGiven an integer n
, return all possible distinct solutions to the N-Queens puzzle, where n
queens are placed on an n x n
chessboard such that no two queens attack each other.
Example:
Input: n = 4 Output: [ [".Q..", // Solution 1 "...Q", "Q...", "..Q."], ["..Q.", // Solution 2 "Q...", "...Q", ".Q.."] ] Explanation: There are two ways to arrange four queens so that they do not attack each other.
Test Cases
Test Cases
Input
4
Expected Output
.Q.. ...Q Q... ..Q. ..Q. Q... ...Q .Q..
Step 1
Step 2
Step 3
Step 4
Step 1: Identify the Pattern
N-Queens
HardGiven an integer n
, return all possible distinct solutions to the N-Queens puzzle, where n
queens are placed on an n x n
chessboard such that no two queens attack each other.
Example:
Input: n = 4 Output: [ [".Q..", // Solution 1 "...Q", "Q...", "..Q."], ["..Q.", // Solution 2 "Q...", "...Q", ".Q.."] ] Explanation: There are two ways to arrange four queens so that they do not attack each other.
Test Cases
Test Cases
Input
4
Expected Output
.Q.. ...Q Q... ..Q. ..Q. Q... ...Q .Q..
Step 1
Step 2
Step 3
Step 4
Step 1: Identify the Pattern