-
Notifications
You must be signed in to change notification settings - Fork 0
/
N-QueensProblem.cpp
87 lines (81 loc) · 3.06 KB
/
N-QueensProblem.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
//program to solve the n queen problem
//grid[][] is represent the 2-d array with value(0 and 1) for grid[i][j]=1 means queen i are placed at j column.
//we can take any number of queen , for this time we take the atmost 10 queen (grid[10][10]).
#include <iostream>
using namespace std;
int grid[10][10];
/*———————————————————————————————————————————————————————————————————————————*/
//print the solution
void print(int n){
for (int i = 0; i <= n-1; i++){
for (int j = 0; j <= n-1; j++){
cout << grid[i][j] << " ";
}
cout << endl;
}
cout << endl;
cout << endl;
}
/*———————————————————————————————————————————————————————————————————————————*/
//function for check the position is safe or not
//row is indicates the queen no. and col represents the possible positions
bool isSafe(int col, int row, int n){
//check for same column
for (int i = 0; i < row; i++){
if (grid[i][col]) {
return false;
}
}
//check for upper left diagonal
for (int i = row, j = col; i >= 0 && j >= 0; i--, j--){
if (grid[i][j]){
return false;
}
}
//check for upper right diagonal
for (int i = row, j = col; i >= 0 && j < n; j++, i--){
if (grid[i][j]){
return false;
}
}
return true;
}
/*———————————————————————————————————————————————————————————————————————————*/
//function to find the position for each queen
//row is indicates the queen no. and col represents the possible positions
bool solve (int n, int row){
if (n == row){
print(n);
return true;
}
//variable res is use for possible backtracking
bool res = false;
for (int i = 0; i <= n - 1; i++){
if (isSafe(i, row, n)){
grid[row][i] = 1;
//recursive call solve(n, row+1) for next queen (row+1)
res = solve(n, row+1) || res;//if res ==false then backtracking will occur by assigning the grid[row][i] = 0
grid[row][i] = 0;
}
}
return res;
}
/*———————————————————————————————————————————————————————————————————————————*/
int main(){
int n;
cout << "Enter the number of queens : ";
cin >> n;
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
grid[i][j] = 0;
}
}
bool res = solve(n, 0);
if(res == false){
cout << -1 << endl; //if there is no possible solution
}
else{
cout << endl;
}
return 0;
}