N Queens
Place n queens on an n by n board so that no two share a row, column or diagonal. Return every board as an array of row strings, with Q for a queen and dots elsewhere. Result order is unrestricted.
Example 1
n = 4[[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]Explanation
The two n=4 boards each use distinct columns and diagonals with one queen per row.
Example 2
n = 2[]Explanation
Two queens on a 2-by-2 board always share a row, column or diagonal.
Example 3
n = 1[["Q"]]Explanation
One queen fits the only cell.
Constraints
- 1 <= n <= 9
Function contract
solveNQueensArguments: jsonSolutionRuns on your device
Loading draft…
Run: 3 examples; Submit: 9 cases, on your device. All 9 legal values of n are covered; duplicate inputs are not counted as extra tests.
4