N Queen Problem Using Branch and Bound in C++
Last Updated :
14 Jul, 2024
N-Queen problem involves placing N queens on an N×N chessboard such that no two queens threaten each other. This means no two queens share the same row, column, or diagonal.
In this article, we will learn to solve the N queen problem using the Branch and Bound technique which provides an efficient method compared to simple backtracking.
Example:
Input:
N = 8
Output:
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
The backtracking Algorithm for N-Queen is already discussed here.
The idea behind the Branch and Bound approach is to enhance the backtracking method by pruning the search space early. In backtracking, we explore each possibility and backtrack when we hit a dead end. However, in Branch and Bound, we can avoid exploring certain branches of the search tree altogether if we know they lead to invalid solutions.
Approach:
- Initialize the Board:
- Create a 2D array to represent the board.
- Initialize arrays to track columns and diagonals.
- Check Safety:
- Create a function isSafe to check if a position is safe by ensuring the current row, and both diagonals are free of other queens.
- Recursive Backtracking:
- Implement the recursive function solve that places queens column by column.
- For each column, try placing a queen in each row and check if it’s safe.
- If placing the queen is safe, mark the position and recursively try to place the next queen in the next column.
- If placing the queen is not safe, backtrack and try the next row.
- Print Solution:
- If a solution is found, print the board.
C++ Program to Solve N-Queen Problem
The below program implements the above approach to solve N-Queen Problem in C++.
C++
// C++ program to solve N Queen problem using Branch and
// Bound approach
#include <iostream>
#include <vector>
using namespace std;
int N;
// Function to print the solution
void printSolution(vector<vector<int> >& board)
{
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
// Print each cell of the board
cout << board[i][j] << " ";
}
// Print new line after each row
cout << endl;
}
}
// Optimized isSafe function to check if current row, left
// diagonal or right diagonal contains any queen
bool isSafe(int row, int col, vector<bool>& rows,
vector<bool>& leftDiagonals,
vector<bool>& rightDiagonals)
{
// Check if the current row, left diagonal, or right
// diagonal is already occupied by a queen
if (rows[row] || leftDiagonals[row + col]
|| rightDiagonals[col - row + N - 1]) {
// If occupied, it's not safe to place the queen
return false;
}
// If not occupied, it's safe to place the queen
return true;
}
// Recursive function to solve N-Queen Problem
bool solve(vector<vector<int> >& board, int col,
vector<bool>& rows, vector<bool>& leftDiagonals,
vector<bool>& rightDiagonals)
{
// Base Case: If all Queens are placed
if (col >= N) {
// All queens are placed successfully
return true;
}
// Consider this column and try placing queen in all
// rows one by one
for (int i = 0; i < N; i++) {
if (isSafe(i, col, rows, leftDiagonals,
rightDiagonals)) {
// Mark the row and diagonals as occupied
rows[i] = true;
leftDiagonals[i + col] = true;
rightDiagonals[col - i + N - 1] = true;
// Place the queen
board[i][col] = 1;
// Recur to place rest of the queens
if (solve(board, col + 1, rows, leftDiagonals,
rightDiagonals)) {
// If placing the queen in the next column
// leads to a solution, return true
return true;
}
// Backtracking: Unmark the row and diagonals,
// and remove the queen
rows[i] = false;
leftDiagonals[i + col] = false;
rightDiagonals[col - i + N - 1] = false;
// Remove the queen
board[i][col] = 0;
}
}
// If no place is safe in the current column, return
// false
return false;
}
int main()
{
// Taking input from the user
cout << "Enter the number of rows for the square "
"board: ";
// Read the size of the board
cin >> N;
// Board of size N*N
// Initialize the board with all cells as 0 (no queens
// placed)
vector<vector<int> > board(N, vector<int>(N, 0));
// Arrays to tell which rows and diagonals are occupied
// Initialize the row markers
vector<bool> rows(N, false);
// Initialize the left diagonal markers
vector<bool> leftDiagonals(2 * N - 1, false);
// Initialize the right diagonal markers
vector<bool> rightDiagonals(2 * N - 1, false);
// Start solving from the first column
bool ans = solve(board, 0, rows, leftDiagonals,
rightDiagonals);
if (ans) {
// If a solution is found, print the solution board
printSolution(board);
}
else {
// If no solution exists, print a message
cout << "Solution does not exist\n";
}
return 0;
}
Output
Enter the number of rows for the square board: 8
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 Complexity: The time complexity of the solver algorithm is O(N!), where N is the number of rows and columns in the square board. This is because for each column, the algorithm tries to place a queen in each row and then recursively tries to place the queens in the remaining columns. The number of possible combinations of queen placements in the board is N! since there can be only one queen in each row and each column.
Space Complexity: The space complexity of the solver algorithm is O(N^2), where N is the number of rows and columns in the square board. This is because we are using a 2D vector to represent the board, which takes up N^2 space. Additionally, we are using three boolean arrays to keep track of the occupied rows and diagonals, which take up 2N-1 space each. Therefore, the total space complexity is O(N^2 + 6N – 3), which is equivalent to O(N^2).
Similar Reads
C++ Programming Language C++ is a computer programming language developed by Bjarne Stroustrup as an extension of the C language. It is known for is fast speed, low level memory management and is often taught as first programming language. It provides:Hands-on application of different programming concepts.Similar syntax to
5 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Object Oriented Programming in C++ Object Oriented Programming - As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so th
5 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
30 OOPs Interview Questions and Answers [2025 Updated] Object-oriented programming, or OOPs, is a programming paradigm that implements the concept of objects in the program. It aims to provide an easier solution to real-world problems by implementing real-world entities such as inheritance, abstraction, polymorphism, etc. in programming. OOPs concept is
15 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read