Tutorialspoint
Problem
Solution
Submissions

N-Queens Problem with Backtracking

Certification: Advanced Level Accuracy: 0% Submissions: 0 Points: 15

Write a C++ program to solve the N-Queens problem using backtracking. The N-Queens problem is to place N chess queens on an N×N chessboard so that no two queens attack each other. Queens can attack horizontally, vertically, and diagonally. The program should find and display one valid solution for placing N queens on the board.

Example 1
  • Input: n = 4
  • Output:
    . Q . .
    . . . Q
    Q . . .
    . . Q .
  • Explanation:
    • We need to place 4 queens on a 4×4 chessboard.
    • Starting from the first row, we place the first queen at position (0,1).
    • For the second row, we find a safe position at (1,3) that doesn't conflict with the first queen.
    • For the third row, we place the queen at (2,0) which is safe from previous queens.
    • Finally, we place the fourth queen at (3,2) completing a valid solution.
Example 2
  • Input: n = 8
  • Output:
    Q . . . . . . .
    . . . . Q . . .
    . . . . . . . Q
    . . . . . Q . .
    . . Q . . . . .
    . . . . . . Q .
    . Q . . . . . .
    . . . Q . . . .
  • Explanation:
    • We need to place 8 queens on an 8×8 chessboard.
    • Using backtracking, we systematically try placing queens in each row.
    • For each position, we check if it's safe from all previously placed queens.
    • If a position leads to no solution, we backtrack and try the next position.
    • The algorithm continues until all 8 queens are successfully placed without conflicts.
Constraints
  • 1 ≤ n ≤ 15
  • You must use backtracking algorithm
  • Display the board with 'Q' for queens and '.' for empty spaces
  • Find and display only one valid solution
  • Time Complexity: O(N!)
  • Space Complexity: O(N²)
RecursionBacktracking AmazonHCL Technologies
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 a 2D vector or array to represent the chessboard
  • Create a recursive function that tries to place queens row by row
  • For each row, try placing a queen in each column and check if it's safe
  • A position is safe if no other queen can attack it (same row, column, or diagonal)
  • If placing a queen leads to no solution, backtrack by removing it and trying the next position
  • Use helper functions to check if a position is safe from attacks
  • Once all N queens are placed successfully, display the solution

Steps to solve by this approach:

 Step 1: Initialize an n×n chessboard with all positions marked as empty (0).
 Step 2: Start with the first row (row = 0) and try to place a queen in each column systematically.
 Step 3: For each position (row, col), check if it's safe by verifying no other queen can attack this position from the same column, upper-left diagonal, and upper-right diagonal.
 Step 4: If the position is safe, place the queen (mark as 1) and recursively try to solve for the next row.
 Step 5: If the recursive call returns true, we have found a valid solution and can return true.
 Step 6: If the recursive call returns false or no safe position is found in the current row, backtrack by removing the queen (mark as 0) and try the next column.
 Step 7: If all rows are successfully filled with queens (base case: row >= n), return true indicating a complete solution is found.

Submitted Code :