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.
Queens can attack horizontally, vertically, and diagonally. Therefore, no two queens can share:
The backtracking approach: place queens row by row. For each row, try each column and check if the placement is safe (no conflicts with already-placed queens). If safe, recurse to next row. If all rows filled, we found a solution.
n = 4[[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]n = 1[["Q"]]1 <= n <= 9Click "Run" to execute your code against test cases
Socratic guidance - I'll ask questions, not give answers