Here is a C++ program that solves the N-Queens problem:
#include <iostream>
#include <vector>
using namespace std;
// Function to print the board
void printBoard(const vector<int>& board) {
int N = board.size();
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (board[i] == j) {
cout << "Q ";
} else {
cout << ". ";
}
}
cout << endl;
}
cout << endl;
}
// Function to check if a queen can be placed at a given position
bool isSafe(const vector<int>& board, int row, int col) {
int N = board.size();
// Check if there is a queen in the same column
for (int i = 0; i < row; i++) {
if (board[i] == col) {
return false;
}
}
// Check if there is a queen in the upper left diagonal
for (int i = row, j = col; i >= 0 && j >= 0; i--, j--) {
if (board[i] == j) {
return false;
}
}
// Check if there is a queen in the upper right diagonal
for (int i = row, j = col; i >= 0 && j < N; i--, j++) {
if (board[i] == j) {
return false;
}
}
return true;
}
// Recursive function to solve the N-queens problem
void solveNQueens(vector<int>& board, int row, vector<vector<int>>& solutions) {
int N = board.size();
// If all queens are placed, add the current solution to the list of solutions
if (row == N) {
solutions.push_back(board);
return;
}
// Try placing a queen in each column of the current row
for (int col = 0; col < N; col++) {
if (isSafe(board, row, col)) {
board[row] = col;
solveNQueens(board, row + 1, solutions);
}
}
}
// Function to solve the N-queens problem
vector<vector<int>> solveNQueens(int N) {
vector<vector<int>> solutions;
vector<int> board(N);
solveNQueens(board, 0, solutions);
return solutions;
}
int main() {
int N;
cout << "Enter the size of the board: ";
cin >> N;
vector<vector<int>> solutions = solveNQueens(N);
cout << "Total solutions: " << solutions.size() << endl;
for (const auto& solution : solutions) {
printBoard(solution);
}
return 0;
}
The program first defines a utility function printBoard to print the board. Next, it defines a function isSafe to check if a queen can be placed at a given position. This function checks if there is a queen in the same column or in any of the diagonals of the current position.
The main logic of the program lies in the solveNQueens function. It is a recursive function that takes the current board, the current row, and a list of solutions as parameters. It first checks if all queens are placed, in which case it adds the current solution to the list of solutions. Otherwise, it tries placing a queen in each column of the current row, and recursively calls itself for the next row. The function backtracks whenever it finds a solution or determines that it is not possible to place a queen at the current position.
Finally, in the main function, the user is prompted to enter the size of the board. The solveNQueens function is called with the given size, and the list of solutions is printed along with the total number of solutions.
Note that the program assumes that the input size is valid, i.e., N >= 4. The N-Queens problem is not solvable for N = 2 and N = 3.