-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8Puzzle.cpp
281 lines (250 loc) · 6.72 KB
/
8Puzzle.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include <bits/stdc++.h>
using namespace std;
class puzzle {
public:
int p[3][3];
int f, g;
puzzle(string st) {
f = 0;
g = 0;
int k=0;
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
p[i][j] = st[k++] - '0';
}
}
}
void display() {
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
cout<<p[i][j]<<" ";
}
cout<<endl;
}
}
string to_string() {
string st = "123456780";
int k=0;
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
st[k++] = p[i][j] + '0';
}
}
return st;
// cout<<"String: "<<st;
}
int tile_mismatch_count() { //PART - 2
int count = 0;
string goal = "123456780";
string cur = to_string();
for(int i=0; i<9; i++) {
if(cur[i] != '0' && goal[i] != cur[i]) {
count++;
}
}
return count;
}
int manhattan_distance() { //PART - 2
int dist = 0;
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
switch(p[i][j]) {
case 1: dist += abs(i - 0) + abs(j - 0); break;
case 2: dist += abs(i - 0) + abs(j - 1); break;
case 3: dist += abs(i - 0) + abs(j - 2); break;
case 4: dist += abs(i - 1) + abs(j - 0); break;
case 5: dist += abs(i - 1) + abs(j - 1); break;
case 6: dist += abs(i - 1) + abs(j - 2); break;
case 7: dist += abs(i - 2) + abs(j - 0); break;
case 8: dist += abs(i - 2) + abs(j - 1); break;
}
}
}
return dist;
}
bool goal_test() {
string goal = "123456780";
string cur = to_string();
if(goal == cur) {
return 1;
}
return 0;
}
void update_f(int h) {
if(!h) {
f = 0;
int tile_count = tile_mismatch_count();
f = tile_count + g;
}
else {
f = 0;
int manh_distance = manhattan_distance();
f = manh_distance + g;
}
}
void initialize() {
f = 0;
g = 0;
}
};
bool operator<(const puzzle& p1, const puzzle& p2) {
return p1.f > p2.f;
}
priority_queue <puzzle> fringe;
unordered_set <string> visited;
int max_len, no_of_nodes;
bool isValid(int i, int j) {
if(i>=0 && j>=0 && i<3 && j<3)
return 1;
else
return 0;
}
puzzle move(puzzle s, int i1, int j1, int i2, int j2) {
string st = s.to_string();
int index1 = i1*3+j1, index2 = i2*3+j2;
char temp_char = st[index1];
st[index1] = st[index2];
st[index2] = temp_char;
puzzle new_puz(st);
return new_puz;
}
void expand(puzzle s, queue <puzzle> &queue_to_insert) {
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
if(s.p[i][j] == 0) {
if(isValid(i-1, j)) {
puzzle new_puz = move(s, i, j, i-1, j);
if(visited.find(new_puz.to_string()) == visited.end()) {
visited.insert(new_puz.to_string());
new_puz.g++;
no_of_nodes++;
// cout<<"Pushed "<<new_puz.to_string()<<endl;
queue_to_insert.push(new_puz);
}
}
if(isValid(i+1, j)) {
puzzle new_puz = move(s, i, j, i+1, j);
if(visited.find(new_puz.to_string()) == visited.end()) {
visited.insert(new_puz.to_string());
new_puz.g++;
no_of_nodes++;
// cout<<"Pushed "<<new_puz.to_string()<<endl;
queue_to_insert.push(new_puz);
}
}
if(isValid(i, j-1)) {
puzzle new_puz = move(s, i, j, i, j-1);
if(visited.find(new_puz.to_string()) == visited.end()) {
visited.insert(new_puz.to_string());
new_puz.g++;
no_of_nodes++;
// cout<<"Pushed "<<new_puz.to_string()<<endl;
queue_to_insert.push(new_puz);
}
}
if(isValid(i, j+1)) {
puzzle new_puz = move(s, i, j, i, j+1);
if(visited.find(new_puz.to_string()) == visited.end()) {
visited.insert(new_puz.to_string());
new_puz.g++;
no_of_nodes++;
// cout<<"Pushed "<<new_puz.to_string()<<endl;
queue_to_insert.push(new_puz);
}
}
}
}
}
}
queue <puzzle> modified_expand(puzzle s, int depth) {
queue <puzzle> temp1;
expand(s, temp1);
queue <puzzle> temp2(temp1);
queue <puzzle> temp3;
depth--;
while(depth>0) {
while(!temp2.empty()) {
puzzle c = temp2.front();
temp2.pop();
expand(c, temp3);
queue <puzzle> temp4(temp3);
while(!temp4.empty()) {
temp1.push(temp4.front());
temp4.pop();
}
}
depth--;
while(!temp3.empty()) {
temp2.push(temp3.front());
temp3.pop();
}
}
return temp1;
}
puzzle aStar(puzzle s, int h, int depth) { //PART - 1
max_len = 0;
no_of_nodes = 0;
s.initialize();
visited.clear();
visited.insert(s.to_string());
fringe = priority_queue <puzzle>();
while(!s.goal_test()) {
queue <puzzle> mod_exp_result = modified_expand(s, depth);
deque <puzzle> temp;
while(!mod_exp_result.empty()) {
temp.push_back(mod_exp_result.front());
mod_exp_result.pop();
}
for(int i = 0; i < temp.size(); i++) {
temp[i].update_f(h);
fringe.push(temp[i]);
if(fringe.size() > max_len) {
max_len = fringe.size();
}
}
if(!fringe.empty()) {
s = fringe.top();
fringe.pop();
}
else break;
// g++;
}
if(s.goal_test())
cout<<"Goal State Reached!"<<endl;
else
cout<<"Goal State not Reached!"<<endl;
cout<<"Maximum fringe length: "<<max_len<<endl; //PART - 5
cout<<"No. of nodes: "<<no_of_nodes<<endl; //PART - 5
return s;
}
int main() {
int depth;
int rand_gen_initial_states = 10; //PART - 4
cout<<"Enter Depth: ";
cin>>depth;
// UNCOMMENT LINES MARKED (1)-(4) AND COMMENT LINES MARKED (5)-(7) FOR GENERATING ONLY VALID INPUT STATES.
// srand ( time(NULL) ); //-----(1)
// const string input_Arr[20] = {"413705826", "813402765", "246583017", "038145672", "301682475", "182043765", "301682475", "361285047", "261705834", "203714568", "501837264", "725368401", "157820643", "713248650", "274518306", "437165280", "452710836", "062537481", "804125376", "235107846"}; //-----(2)
while(rand_gen_initial_states--) { //PART - 4
// int RandIndex = rand() % 20; //-----(3)
// puzzle s(input_Arr[RandIndex]); //-----(4)
string input = "123456780"; //-----(5)
random_shuffle(input.begin(), input.end()); //-----(6) //PART - 4
puzzle s(input); //-----(7)
cout<<endl<<"-----------------------------------------------------"<<endl;
cout<<" ROUND "<<10-rand_gen_initial_states;
cout<<endl<<"-----------------------------------------------------"<<endl;
cout<<"Initial State: "<<endl;
s.display();
cout<<endl;
cout<<endl<<"Depth 1 using Tile Mismatch Count: "<<endl;
aStar(s, 0, 1); //PART - 3
cout<<endl<<"Depth "<<depth<<" using Tile Mismatch Count: "<<endl;
aStar(s, 0, depth); //PART - 3
cout<<endl<<"Depth 1 using Manhattan Distance: "<<endl;
aStar(s, 1, 1); //PART - 3
cout<<endl<<"Depth "<<depth<<" using Manhattan Distance: "<<endl;
aStar(s, 1, depth); //PART - 3
}
return 0;
}