-
Notifications
You must be signed in to change notification settings - Fork 1
/
078NQueensProblem.cpp
105 lines (85 loc) · 2.48 KB
/
078NQueensProblem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//program to solve N Queens problem
// including required header files
#include <iostream>
#include <cmath>
using namespace std;
// initailizing max length of the queen board
const int MAX_N = 10;
// function to display solution
void printSolution(int board[MAX_N][MAX_N], int N) {
int solutionArr[N];
int solutionCount = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (board[i][j] == 1){
cout << "Q ";
solutionArr[solutionCount] = j;
solutionCount++;
} else {
cout << "* ";
}
}
cout << endl;
}
cout << endl;
cout << "The array of solutions is: [ ";
for (int i = 0; i < N; i++) {
if (i < N-1) {
cout << solutionArr[i] << ", ";
} else {
cout << solutionArr[i] << " ]" << endl;
}
}
}
// function to check weather the next move to safe to take or not for the queen
bool isSafe(int board[MAX_N][MAX_N], int row, int col, int N) {
int i, j;
for (i = 0; i < col; i++) {
if (board[row][i])
return false;
}
for (i = row, j = col; i >= 0 && j >= 0; i--, j--) {
if (board[i][j])
return false;
}
for (i = row, j = col; i < N && j >= 0; i++, j--) {
if (board[i][j])
return false;
}
return true;
}
// function to solve the for the N queens
bool solveNQueens(int board[MAX_N][MAX_N], int col, int N) {
if (col >= N)
return true;
for (int i = 0; i < N; i++) {
if (isSafe(board, i, col, N)) {
board[i][col] = 1;
if (solveNQueens(board, col + 1, N))
return true;
board[i][col] = 0;
}
}
return false;
}
// main function
int main() {
// input size of the baord
int N;
cout << "Enter the value of N for N-Queens (N should be in between 1 to " << MAX_N << "): ";
cin >> N;
if (N < 1 || N > MAX_N) {
cout << "Invalid input! Please enter a value between 1 and " << MAX_N << ".\n";
return 1;
}
// initailizing the baord
int board[MAX_N][MAX_N] = {0};
// soliving for N queens, displaying the board and displaying the number of solutions
if (solveNQueens(board, 0, N)) {
cout << "Solution for N-Queens with N = " << N << ":" << endl << endl;
printSolution(board, N);
} else {
cout << "No solution found for N = " << N << "." << endl;
}
return 0;
}