N Queen Problem

What is N-Queen Problem?

The n-queens puzzle is about placing n queens on an n x n chessboard so that no two queens can attack each other.

Queens can attack in three ways: horizontally, vertically, and diagonally.

Your task is to find all possible ways to place the queens on the board, ensuring no two queens attack each other.

Each solution will show a unique board arrangement where ‘Q’ represents a queen and ‘.’ represents an empty square. The solutions can be returned in any order.

N Queen problem for Chess

Example 1:

Example of N Queen problem

Explanation: There are two different solutions to the 4-queens puzzle.

Constraints:

  • 1 <= n <= 8

N Queen Problem Solution

Recommendation for Time and Space Complexity – The solution should aim for O(n!) time and O(n²) space, where n is the size of the chessboard.

Hints for solving problems

Hint 1 :

A queen can attack in 8 directions, but we only need to ensure no two queens are in the same row, column, or diagonals. Place one queen per row and try each column. Use a recursive approach to explore all valid ways to position the queens.

Hint 2 :

Use backtracking to explore the chessboard column by column. Start with an empty board and place a queen in a valid cell. If you successfully place queens in all columns, add the board configuration to the result.

Hint 3 :

Begin with an empty board and check each cell in the current column to see if it’s safe to place a queen. A cell is valid if no queens exist in the same row, left diagonal, or left bottom diagonal. Place the queen, continue the recursive search, and backtrack by removing the queen to explore other options.

There are mainly 4 approach to solve this problem-

  1. Backtracking Method
  2. Backtracking(Hash Set) Method
  3. Backtracking(Visited Array) Method
  4. Backtracking(Bit Mask) Method

1. Backtracking Method

This method uses recursion to place queens row by row while ensuring no two queens attack each other. At each step, we check the validity of placing a queen by verifying the current row, column, and diagonals. If a valid placement is found, the algorithm continues recursively; otherwise, it backtracks.

  • Time complexity: O(n!)
  • Space complexity: O(n^2)

Code

2. Backtracking(Hash Set) Method

Here, we use hash sets to track the columns and diagonals already occupied by queens. These sets allow quick lookups to check if a cell is safe for placing a queen. The method reduces redundant checks and improves efficiency.

  • Time complexity: O(n!)
  • Space complexity: O(n^2)

Code

3. Backtracking(Visited Array) Method

Instead of hash sets, this method uses arrays to mark visited columns and diagonals. One array tracks columns, and two additional arrays track the left and right diagonals. This approach is similar to the hash set method but more space-efficient.

  • Time complexity: O(n!)
  • Space complexity: O(n^2)

Code

4. Backtracking(Bit Mask) Method

This optimized approach uses bit masking to track columns and diagonals, making it extremely space-efficient. By representing occupied columns and diagonals as bits in integers, we can quickly check and update states during recursion, making it suitable for larger board sizes.

  • Time complexity: O(n!)
  • Space complexity: O(n^2)

Code

More Articles