
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Solve N-Queen Problem
The N-Queens problem is a puzzle where we need to place N queens on an N x N chessboard such that no two queens attack each other. A queen will attack another queen if they are in the same row, column, or diagonal. In this problem, you are given a value of N, and you need find possible arrangements for N queens on N x N chessboard.
For example, Consider that we have a chessboard of size 4 x 4. In this case, we can place maximum 4 queens on the chessboard such that no two queens attack each other. The arrangement will look like this:

Backtracking Algorithm to Solve N-Queens Problem
To solve the N-Queens problem, we can use the backtracking algorithm. This algorithm will starts from the leftmost cell and try to place a queen in subsequent cells of that row. A queen will be placed in a cell if it is safe to place (meaning, no other queen is attacking that cell). There will be two cases for each cell:
- Safe to place Queen: In this case, the queen will be placed in the cell and we will then move to the next row as no two queens can be placed in the same row.
- Not safe to place Queen: Another queen is attacking that cell, so we can't place a queen in that cell. We need to backtrack and try the next cell in the same row. If no safe cells are available in that row, we will backtrack to the previous row and try the next cell in that row.
We will repeat this process for all rows until we reach the last row. If we are able to place a queen in the last row, it means we have found a solution.
Functions Used in N-Queens Problem
To solve the N-Queens problem, we need to define two functions:
1. isValid(board, row, col)
This function is used to check if it is safe to place a queen at the given cell (row, col) in the chessboard. It checks for the following conditions:
Begin if there is a queen at the left of current col, then return false if there is a queen at the left upper diagonal, then return false if there is a queen at the left lower diagonal, then return false; return true //otherwise it is valid place End
2. solveNQueen(board, col)
This function implements the backtracking algorithm to solve the N-Queens problem. It tries to place a queen in each row of the current column and recursively calls itself for the next column.
Begin if all columns are filled, then return true for each row of the board, do if isValid(board, i, col), then set queen at place (i, col) in the board if solveNQueen(board, col+1) = true, then return true otherwise remove queen from place (i, col) from board. done return false End
C++ Code to Solve N-Queens Problem
The code below implements the N-Queens problem in C++. The output of the code will be a binary matrix representing the chessboard, where 1 stands for a queen and 0 stands for an empty cell.
#include <iostream> using namespace std; #define N 8 // Size of the chessboard bool isValid(int board[N][N], int row, int col) { // Check the left side of the current cell for (int i = 0; i < col; i++) { if (board[row][i]) { return false; } } // Check the upper diagonal on the left side for (int i = row, j = col; i >= 0 && j >= 0; i--, j--) { if (board[i][j]) { return false; } } // Check the lower diagonal on the left side for (int i = row, j = col; j >= 0 && i < N; i++, j--) { if (board[i][j]) { return false; } } return true; } bool solveNQueen(int board[N][N], int col) { // If all columns are filled, return true if (col >= N) { return true; } // Try placing a queen in each row of the current column for (int i = 0; i < N; i++) { if (isValid(board, i, col)) { // Place the queen at (i, col) board[i][col] = 1; // Recur to place the next queen if (solveNQueen(board, col + 1)) { return true; } // If placing the queen doesn't lead to a solution, remove it board[i][col] = 0; } } return false; // No valid placement found } void printSolution(int board[N][N]) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cout << board[i][j] << " "; } cout << endl; } } int main() { int board[N][N] = {0}; // Initialize the chessboard with 0s if (solveNQueen(board, 0)) { printSolution(board); // Print the solution } else { cout << "No solution exists" << endl; } return 0; }
The output of the above code will be:
1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0
Time and Space Complexity of N-Queens Problem
Time Complexity: The time complexity of this algorithm is O(N!) because we are trying to place N queens in N rows and for each row, we are checking N columns. The worst-case scenario is when we have to backtrack for all placements.
Space Complexity: The space complexity of this algorithm is O(N^2) because we are using a 2D array of size N x N to represent the chessboard.
Note: We can optimize the space complexity to O(N) by using three arrays col[], diag1[], and diag2[] to keep track of the columns and diagonals that are under attack.