Tutorialspoint
Problem
Solution
Submissions

N-Queens Problem

Certification: Advanced Level Accuracy: 100% Submissions: 1 Points: 15

Write a Java program to solve the N-Queens puzzle. The N-Queens puzzle is the problem of placing N chess queens on an N×N chessboard so that no two queens threaten each other. Thus, a solution requires that no two queens share the same row, column, or diagonal.

Return all distinct solutions to the N-Queens puzzle as a list of lists. Each solution contains a distinct board configuration of the N-Queens placement, where 'Q' indicates a queen and '.' indicates an empty space.

Example 1
  • Input: n = 4
  • Output: [ [".Q..", "...Q", "Q...", "..Q."], ["..Q.", "Q...", "...Q", ".Q.."] ]
  • Explanation:
    Step 1: For a 4×4 board, we need to place 4 queens.
    Step 2: In the first solution, queens are placed at positions (0,1), (1,3), (2,0), and (3,2).
    Step 3: In the second solution, queens are placed at positions (0,2), (1,0), (2,3), and (3,1).
    Step 4: Both solutions ensure no two queens threaten each other.
Example 2
  • Input: n = 1
  • Output: [["Q"]]
  • Explanation:
    Step 1: For a 1×1 board, there is only one place to put the queen.
    Step 2: The only solution is to place a queen at position (0,0).
Constraints
  • 1 <= n <= 9
  • Each solution must contain n queens on an n×n board
  • Time Complexity: O(N!), where N is the number of queens
  • Space Complexity: O(N^2)
Backtracking PwCZomato
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use backtracking to explore all possible placements.
  • Start placing queens row by row, trying each column in a row.
  • Before placing a queen, check if the position is under attack by any previously placed queens.
  • To check if a position is safe, verify no queens exist in the same column, diagonal, or anti-diagonal.
  • Use arrays or sets to track occupied columns and diagonals.
  • If you reach the last row successfully, you've found a valid solution. Add it to the result.
  • After exploring all possibilities with a queen at a position, backtrack and try the next position.

Steps to solve by this approach:

 Step 1: Initialize an empty n×n chessboard filled with '.' characters.

 Step 2: Create sets to track occupied columns and diagonals.
 Step 3: Start backtracking from the first row (row 0).
 Step 4: For each column in the current row, check if placing a queen is safe.
 Step 5: If safe, place the queen, mark the column and diagonals as occupied, and move to the next row.
 Step 6: If all queens are placed (reached row n), add the current board configuration to results.
 Step 7: After exploring all possibilities with a queen at the current position, backtrack by removing the queen and its constraints from the tracking sets.

Submitted Code :