-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8-queen.cpp
More file actions
104 lines (87 loc) · 2.54 KB
/
8-queen.cpp
File metadata and controls
104 lines (87 loc) · 2.54 KB
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
/**
* NOTICE:- Working fine on https://www.onlinegdb.com/ , but may face some error on compiling locally.
* Please try it out on https://www.onlinegdb.com/ , if not working locally.
**/
#include<bits/stdc++.h>
using namespace std;
struct Path{
vector<int> path;
};
struct Result{
vector< vector<int> > results;
};
class comparision{
public:
bool operator()( Path p1, Path p2) const
{
return p1.path.size() < p2.path.size();
}
};
class Queen{
public:
priority_queue< Path, vector< Path>, comparision> pq;
Result solutions;
Queen(){
}
void UCS(){
for(int i=0; i<8; ++i){
Path tpath;
tpath.path.push_back(i);
pq.push(tpath);
}
while(!pq.empty()){
Path tpath = pq.top();
pq.pop();
//current tpath is a goal
if(isGoal(tpath.path)){
solutions.results.push_back(tpath.path);
};
for(int i=0; i<8; ++i){
Path tpath1 = tpath;
tpath1.path.push_back(i);
cout<<endl;
if(isValid(tpath1.path)){
pq.push(tpath1);
}
}
}
}
bool isGoal(vector<int> path){
return path.size()==8;
}
bool isValid(vector<int> path){
for(int i=0; i<path.size()-1; ++i){
//check column
if(path[path.size()-1] == path[i]){
return false;
}
// check diagonal
if( abs( path[path.size()-1]-path[i] )== abs( path.size()-1 - i ) ){
return false;
}
}
return true;
}
void printResult(){
for(int i=0; i<solutions.results.size(); ++i){
for(int j=0; j<8; ++j){
for(int k=0; k<8; ++k){
if(solutions.results[i][j] == k){
cout<<" Q ";
}else{
cout<<" - ";
}
}
cout<<endl;
}
cout<<endl;
}
cout<<"\nTotal solutions: "<<solutions.results.size()<<endl;
}
};
int main(){
Queen queen;
queen.UCS();
queen.printResult();
return 0;
}