N-Queens - Problem
The N-Queens Problem is a classic chess puzzle that challenges you to place
In chess, a queen can attack any piece in the same row, column, or diagonal. Your task is to find all possible arrangements where
Input: An integer
Output: A list of all distinct solutions, where each solution is represented as a list of strings. Each string represents a row of the chessboard, with
For example, with
n queens on an n ร n chessboard such that no two queens can attack each other.In chess, a queen can attack any piece in the same row, column, or diagonal. Your task is to find all possible arrangements where
n queens coexist peacefully on the board.Input: An integer
n representing the size of the chessboardOutput: A list of all distinct solutions, where each solution is represented as a list of strings. Each string represents a row of the chessboard, with
'Q' indicating a queen and '.' indicating an empty space.For example, with
n = 4, one valid solution would place queens at positions that don't threaten each other, creating a safe configuration for all pieces. Input & Output
example_1.py โ Python
$
Input:
n = 4
โบ
Output:
[[".Q..","...Q","Q...","..Q."],
["..Q.","Q...","...Q",".Q.."]]
๐ก Note:
For a 4ร4 board, there are exactly 2 distinct solutions. In the first solution, queens are placed at (0,1), (1,3), (2,0), (3,2). In the second solution, queens are at (0,2), (1,0), (2,3), (3,1). Both arrangements ensure no two queens can attack each other.
example_2.py โ Python
$
Input:
n = 1
โบ
Output:
[["Q"]]
๐ก Note:
For a 1ร1 board, there's only one solution: place the single queen in the only available position. This is the trivial base case.
example_3.py โ Python
$
Input:
n = 2
โบ
Output:
[]
๐ก Note:
For a 2ร2 board, it's impossible to place 2 queens without them attacking each other. Any placement will result in queens sharing the same row, column, or diagonal, so the result is an empty list.
Visualization
Tap to expand
Understanding the Visualization
1
Initialize Board
Start with empty 4ร4 board and tracking arrays for columns and diagonals
2
Place First Queen
Place queen in row 0, try each column until finding valid position
3
Recursive Placement
Move to next row, place queen avoiding conflicts with previous queens
4
Backtrack on Conflict
When no valid position exists, remove last queen and try next position
5
Solution Found
When all rows filled successfully, record the solution and continue search
Key Takeaway
๐ฏ Key Insight: Backtracking with constraint tracking reduces the search space from n! permutations to a much smaller tree by eliminating invalid partial solutions early.
Time & Space Complexity
Time Complexity
O(N!)
In the worst case, we might need to try N positions in first row, N-2 in second, etc., leading to roughly N! combinations, but with heavy pruning
โ Linear Growth
Space Complexity
O(N)
Recursion stack depth is N, plus arrays to track column and diagonal usage
โ Linear Space
Constraints
- 1 โค n โค 9
- Each solution contains a distinct board configuration
- Solutions can be returned in any order
- Board representation uses 'Q' for queens and '.' for empty spaces
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code