-
Notifications
You must be signed in to change notification settings - Fork 0
/
N Puzzle(A star).cpp
519 lines (515 loc) · 24 KB
/
N Puzzle(A star).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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/****************************************************************************************************************************************************************
Author:
Dhairya Dhondiyal
Description:
The program solves N-Puzzle using A* algorithm with manhattan distance + linear conflict as heuristics.
Disclaimer:
Copyright (C) - All Rights Reserved
Unauthorized copying of this file, via any medium is strictly prohibited
Proprietary and confidential
Written by Dhairya Dhondiyal, February 2016
****************************************************************************************************************************************************************/
#include<iostream>
#include<vector>
#include<math.h>
#include<set>
#include<queue>
#include<time.h>
#define N 4
using namespace std;
int grid[N][N]={{2,8,7,11}, //42 Moves
{5,0,4,15},
{13,9,14,3},
{1,10,6,12}};
/*int grid[N][N]= {{10,11,6,12}, //65 Moves , Runs out of memory,less nodes generated
{15,13,0,9},
{8,14,2,3},
{7,4,1,5}};*/
/*int grid[N][N]= {{3,1,10,13}, //61 Moves, Runs out of memory,more nodes generated
{5,14,0,6},
{12,11,2,9},
{8,4,7,15}};*/
/*int grid[N][N]={{8,6,7}, //31 Moves, Toughest 8-Puzzle
{2,5,4},
{3,0,1}};*/
/*int grid[N][N]={{1,7,0,9}, //46 Moves
{3,13,4,8},
{5,11,2,15},
{12,14,10,6}};*/
/*int grid[N][N]={{0,1,3,4}, //34 Moves
{10,2,9,15},
{13,12,8,6},
{7,14,11,5}};*/
/*int grid[N][N]={{9,8,13,2}, //43 Moves
{0,3,7,4},
{14,10,1,12},
{5,6,11,15}};*/
/*int grid[N][N]={{14,9,1,8},
{7,13,6,2},
{5,10,12,0},
{15,4,3,11}};*/
struct Node //Basic Node
{
int x0,y0,g,h,f,manhattan_h;
int Grid[N][N];
Node* parent=NULL;
Node(int x=0,int y=0,int G=0,Node* node=NULL)
{
x0=x;
y0=y;
g=G;
parent=node;
}
}start,*process;
int f,i,j,ctr,siz,original_h,exchanged_h,maxi,maxj,linear_horizontal_conflicts,linear_vertical_conflicts;
struct compareNode //Custom Comparison for Node min(f) and reprioritizing
{
bool operator()(const Node* Node1,const Node* Node2)const
{
if(Node1->f!=Node2->f)
return Node1->f<Node2->f;
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
if(Node1->Grid[i][j]!=Node2->Grid[i][j])
{
return Node1->Grid[i][j]<Node2->Grid[i][j];
}
}
}
return false;
}
};
struct GridLess //custom comparison for Node* type
{
bool operator()(const Node *a,const Node *b) const
{
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
if(a->Grid[i][j]!=b->Grid[i][j])
{
return a->Grid[i][j]<b->Grid[i][j];
}
}
}
return false;
}
};
multiset<Node*,compareNode>open_list_dup; //For O(1) search time min(f) More nodes N=4
set<Node*,GridLess>closed_list,open_list; //For O(log(n)) search time.
set<Node*,GridLess>::const_iterator open_list_cpy;
clock_t tStart,tEnd;
vector<Node*> neighbors;
Neighbors(Node* temp_Node) //Uses Manhattan Heuristics and linear conflict
{
siz=0;linear_horizontal_conflicts=0;linear_vertical_conflicts=0;
if(temp_Node->x0+1<=N) //
siz++; //
if(temp_Node->x0-1>0) //
siz++; //
if(temp_Node->y0+1<=N) // Resize vector neighbors
siz++; //
if(temp_Node->y0-1>0) //
siz++; //
neighbors.resize(siz); //
if(temp_Node->x0+1<=N) //Space->Right, Move=Left;
{
neighbors[siz-1]=(new Node(temp_Node->x0+1,temp_Node->y0,temp_Node->g+1,temp_Node));
for(i=0;i<N;i++)
{
maxi=-1;
maxj=-1;
for(j=0;j<N;j++)
{
if(i==temp_Node->y0-1&&j==temp_Node->x0-1)
neighbors.at(siz-1)->Grid[i][j]=temp_Node->Grid[i][j+1];
else if(i==temp_Node->y0-1&&j==temp_Node->x0)
neighbors.at(siz-1)->Grid[i][j]=temp_Node->Grid[i][j-1];
else
neighbors.at(siz-1)->Grid[i][j]=temp_Node->Grid[i][j];
if((i==temp_Node->y0-1&&j==temp_Node->x0-1))
{
if(temp_Node->Grid[i][j+1]!=0&&(temp_Node->Grid[i][j+1]-1)/N==i)
if(temp_Node->Grid[i][j+1]>maxi)
maxi=temp_Node->Grid[i][j+1];
else
linear_horizontal_conflicts++;
}
else if(i==temp_Node->y0-1&&j==temp_Node->x0)
{
if(temp_Node->Grid[i][j-1]!=0&&(temp_Node->Grid[i][j-1]-1)/N==i)
if(temp_Node->Grid[i][j-1]>maxi)
maxi=temp_Node->Grid[i][j-1];
else
linear_horizontal_conflicts++;
}
else
{
if(temp_Node->Grid[i][j]!=0&&(temp_Node->Grid[i][j]-1)/N==i)
if(temp_Node->Grid[i][j]>maxi)
maxi=temp_Node->Grid[i][j];
else
linear_horizontal_conflicts++;
}
if((j==temp_Node->y0-1&&i==temp_Node->x0-1))
{
if(temp_Node->Grid[j][i+1]!=0&&(temp_Node->Grid[j][i+1]-1)%N==i)
if(temp_Node->Grid[j][i+1]>maxj)
maxj=temp_Node->Grid[j][i+1];
else
linear_vertical_conflicts++;
}
else if(j==temp_Node->y0-1&&i==temp_Node->x0)
{
if(temp_Node->Grid[j][i-1]!=0&&(temp_Node->Grid[j][i-1]-1)%N==i)
if(temp_Node->Grid[j][i-1]>maxj)
maxj=temp_Node->Grid[j][i-1];
else
linear_vertical_conflicts++;
}
else
if(temp_Node->Grid[j][i]!=0&&(temp_Node->Grid[j][i]-1)%N==i)
if(temp_Node->Grid[j][i]>maxj)
maxj=temp_Node->Grid[j][i];
else
linear_vertical_conflicts++;
}
}
original_h=abs(temp_Node->y0-1-(temp_Node->Grid[temp_Node->y0-1][temp_Node->x0]-1)/N)+abs(temp_Node->x0-(temp_Node->Grid[temp_Node->y0-1][temp_Node->x0]-1)%N);
exchanged_h=abs(temp_Node->y0-1-(temp_Node->Grid[temp_Node->y0-1][temp_Node->x0]-1)/N)+abs(temp_Node->x0-1-(temp_Node->Grid[temp_Node->y0-1][temp_Node->x0]-1)%N);
neighbors.at(siz-1)->manhattan_h=temp_Node->manhattan_h+exchanged_h-original_h;
neighbors.at(siz-1)->h=neighbors.at(siz-1)->manhattan_h+2*linear_vertical_conflicts+2*linear_horizontal_conflicts;
neighbors.at(siz-1)->f=neighbors.at(siz-1)->h+neighbors.at(siz-1)->g;
siz--;
}
if(temp_Node->x0-1>0) //Space->Left, Move=Right;
{
linear_horizontal_conflicts=0;linear_vertical_conflicts=0;
neighbors[siz-1]=(new Node(temp_Node->x0-1,temp_Node->y0,temp_Node->g+1,temp_Node));
for(i=0;i<N;i++)
{
maxi=-1;
maxj=-1;
for(j=0;j<N;j++)
{
if(i==temp_Node->y0-1&&j==temp_Node->x0-1)
neighbors.at(siz-1)->Grid[i][j]=temp_Node->Grid[i][j-1];
else if(i==temp_Node->y0-1&&j==temp_Node->x0-2)
neighbors.at(siz-1)->Grid[i][j]=temp_Node->Grid[i][j+1];
else
neighbors.at(siz-1)->Grid[i][j]=temp_Node->Grid[i][j];
if((i==temp_Node->y0-1&&j==temp_Node->x0-1))
{
if(temp_Node->Grid[i][j-1]!=0&&(temp_Node->Grid[i][j-1]-1)/N==i)
if(temp_Node->Grid[i][j-1]>maxi)
maxi=temp_Node->Grid[i][j-1];
else
linear_horizontal_conflicts++;
}
else if(i==temp_Node->y0-1&&j==temp_Node->x0-2)
{
if(temp_Node->Grid[i][j+1]!=0&&(temp_Node->Grid[i][j+1]-1)/N==i)
if(temp_Node->Grid[i][j+1]>maxi)
maxi=temp_Node->Grid[i][j+1];
else
linear_horizontal_conflicts++;
}
else
{
if(temp_Node->Grid[i][j]!=0&&(temp_Node->Grid[i][j]-1)/N==i)
if(temp_Node->Grid[i][j]>maxi)
maxi=temp_Node->Grid[i][j];
else
linear_horizontal_conflicts++;
}
if((j==temp_Node->y0-1&&i==temp_Node->x0-1))
{
if(temp_Node->Grid[j][i-1]!=0&&(temp_Node->Grid[j][i-1]-1)%N==i)
if(temp_Node->Grid[j][i-1]>maxj)
maxj=temp_Node->Grid[j][i-1];
else
linear_vertical_conflicts++;
}
else if(j==temp_Node->y0-1&&i==temp_Node->x0-2)
{
if(temp_Node->Grid[j][i+1]!=0&&(temp_Node->Grid[j][i+1]-1)%N==i)
if(temp_Node->Grid[j][i+1]>maxj)
maxj=temp_Node->Grid[j][i+1];
else
linear_vertical_conflicts++;
}
else
if(temp_Node->Grid[j][i]!=0&&(temp_Node->Grid[j][i]-1)%N==i)
if(temp_Node->Grid[j][i]>maxj)
maxj=temp_Node->Grid[j][i];
else
linear_vertical_conflicts++;
}
}
original_h=abs(temp_Node->y0-1-(temp_Node->Grid[temp_Node->y0-1][temp_Node->x0-2]-1)/N)+abs(temp_Node->x0-2-(temp_Node->Grid[temp_Node->y0-1][temp_Node->x0-2]-1)%N);
exchanged_h=abs(temp_Node->y0-1-(temp_Node->Grid[temp_Node->y0-1][temp_Node->x0-2]-1)/N)+abs(temp_Node->x0-1-(temp_Node->Grid[temp_Node->y0-1][temp_Node->x0-2]-1)%N);
neighbors.at(siz-1)->manhattan_h=temp_Node->manhattan_h+exchanged_h-original_h;
neighbors.at(siz-1)->h=neighbors.at(siz-1)->manhattan_h+2*linear_vertical_conflicts+2*linear_horizontal_conflicts;
neighbors.at(siz-1)->f=neighbors.at(siz-1)->h+neighbors.at(siz-1)->g;
siz--;
}
if(temp_Node->y0+1<=N) //Space->Down Move=Up
{
linear_horizontal_conflicts=0;linear_vertical_conflicts=0;
neighbors[siz-1]=(new Node(temp_Node->x0,temp_Node->y0+1,temp_Node->g+1,temp_Node));
for(i=0;i<N;i++)
{
maxi=-1;
maxj=-1;
for(j=0;j<N;j++)
{
if(i==temp_Node->y0-1&&j==temp_Node->x0-1)
neighbors.at(siz-1)->Grid[i][j]=temp_Node->Grid[i+1][j];
else if(i==temp_Node->y0&&j==temp_Node->x0-1)
neighbors.at(siz-1)->Grid[i][j]=temp_Node->Grid[i-1][j];
else
neighbors.at(siz-1)->Grid[i][j]=temp_Node->Grid[i][j];
if((i==temp_Node->y0-1&&j==temp_Node->x0-1))
{
if(temp_Node->Grid[i+1][j]!=0&&(temp_Node->Grid[i+1][j]-1)/N==i)
if(temp_Node->Grid[i+1][j]>maxi)
maxi=temp_Node->Grid[i+1][j];
else
linear_horizontal_conflicts++;
}
else if(i==temp_Node->y0&&j==temp_Node->x0-1)
{
if(temp_Node->Grid[i-1][j]!=0&&(temp_Node->Grid[i-1][j]-1)/N==i)
if(temp_Node->Grid[i-1][j]>maxi)
maxi=temp_Node->Grid[i-1][j];
else
linear_horizontal_conflicts++;
}
else
{
if(temp_Node->Grid[i][j]!=0&&(temp_Node->Grid[i][j]-1)/N==i)
if(temp_Node->Grid[i][j]>maxi)
maxi=temp_Node->Grid[i][j];
else
linear_horizontal_conflicts++;
}
if((j==temp_Node->y0-1&&i==temp_Node->x0-1))
{
if(temp_Node->Grid[j+1][i]!=0&&(temp_Node->Grid[j+1][i]-1)%N==i)
if(temp_Node->Grid[j+1][i]>maxj)
maxj=temp_Node->Grid[j+1][i];
else
linear_vertical_conflicts++;
}
else if(j==temp_Node->y0&&i==temp_Node->x0-1)
{
if(temp_Node->Grid[j-1][i]!=0&&(temp_Node->Grid[j-1][i]-1)%N==i)
if(temp_Node->Grid[j-1][i]>maxj)
maxj=temp_Node->Grid[j-1][i];
else
linear_vertical_conflicts++;
}
else
if(temp_Node->Grid[j][i]!=0&&(temp_Node->Grid[j][i]-1)%N==i)
if(temp_Node->Grid[j][i]>maxj)
maxj=temp_Node->Grid[j][i];
else
linear_vertical_conflicts++;
}
}
original_h=abs(temp_Node->y0-(temp_Node->Grid[temp_Node->y0][temp_Node->x0-1]-1)/N)+abs(temp_Node->x0-1-(temp_Node->Grid[temp_Node->y0][temp_Node->x0-1]-1)%N);
exchanged_h=abs(temp_Node->y0-1-(temp_Node->Grid[temp_Node->y0][temp_Node->x0-1]-1)/N)+abs(temp_Node->x0-1-(temp_Node->Grid[temp_Node->y0][temp_Node->x0-1]-1)%N);
neighbors.at(siz-1)->manhattan_h=temp_Node->manhattan_h+exchanged_h-original_h;
neighbors.at(siz-1)->h=neighbors.at(siz-1)->manhattan_h+2*linear_vertical_conflicts+2*linear_horizontal_conflicts;
neighbors.at(siz-1)->f=neighbors.at(siz-1)->h+neighbors.at(siz-1)->g;
siz--;
}
if(temp_Node->y0-1>0) //Space->Up Move=Down
{
linear_horizontal_conflicts=0;linear_vertical_conflicts=0;
neighbors[siz-1]=(new Node(temp_Node->x0,temp_Node->y0-1,temp_Node->g+1,temp_Node));
for(i=0;i<N;i++)
{
maxi=-1;
maxj=-1;
for(j=0;j<N;j++)
{
if(i==temp_Node->y0-1&&j==temp_Node->x0-1)
neighbors.at(siz-1)->Grid[i][j]=temp_Node->Grid[i-1][j];
else if(i==temp_Node->y0-2&&j==temp_Node->x0-1)
neighbors.at(siz-1)->Grid[i][j]=temp_Node->Grid[i+1][j];
else
neighbors.at(siz-1)->Grid[i][j]=temp_Node->Grid[i][j];
if((i==temp_Node->y0-1&&j==temp_Node->x0-1))
{
if(temp_Node->Grid[i-1][j]!=0&&(temp_Node->Grid[i-1][j]-1)/N==i)
if(temp_Node->Grid[i-1][j]>maxi)
maxi=temp_Node->Grid[i-1][j];
else
linear_horizontal_conflicts++;
}
else if(i==temp_Node->y0-2&&j==temp_Node->x0-1)
{
if(temp_Node->Grid[i+1][j]!=0&&(temp_Node->Grid[i+1][j]-1)/N==i)
if(temp_Node->Grid[i+1][j]>maxi)
maxi=temp_Node->Grid[i+1][j];
else
linear_horizontal_conflicts++;
}
else
{
if(temp_Node->Grid[i][j]!=0&&(temp_Node->Grid[i][j]-1)/N==i)
if(temp_Node->Grid[i][j]>maxi)
maxi=temp_Node->Grid[i][j];
else
linear_horizontal_conflicts++;
}
if((j==temp_Node->y0-1&&i==temp_Node->x0-1))
{
if(temp_Node->Grid[j-1][i]!=0&&(temp_Node->Grid[j-1][i]-1)%N==i)
if(temp_Node->Grid[j-1][i]>maxj)
maxj=temp_Node->Grid[j-1][i];
else
linear_vertical_conflicts++;
}
else if(j==temp_Node->y0-2&&i==temp_Node->x0-1)
{
if(temp_Node->Grid[j+1][i]!=0&&(temp_Node->Grid[j+1][i]-1)%N==i)
if(temp_Node->Grid[j+1][i]>maxj)
maxj=temp_Node->Grid[j+1][i];
else
linear_vertical_conflicts++;
}
else
if(temp_Node->Grid[j][i]!=0&&(temp_Node->Grid[j][i]-1)%N==i)
if(temp_Node->Grid[j][i]>maxj)
maxj=temp_Node->Grid[j][i];
else
linear_vertical_conflicts++;
}
}
original_h=abs(temp_Node->y0-2-(temp_Node->Grid[temp_Node->y0-2][temp_Node->x0-1]-1)/N)+abs(temp_Node->x0-1-(temp_Node->Grid[temp_Node->y0-2][temp_Node->x0-1]-1)%N);
exchanged_h=abs(temp_Node->y0-1-(temp_Node->Grid[temp_Node->y0-2][temp_Node->x0-1]-1)/N)+abs(temp_Node->x0-1-(temp_Node->Grid[temp_Node->y0-2][temp_Node->x0-1]-1)%N);
neighbors.at(siz-1)->manhattan_h=temp_Node->manhattan_h+exchanged_h-original_h;
neighbors.at(siz-1)->h=neighbors.at(siz-1)->manhattan_h+2*linear_vertical_conflicts+2*linear_horizontal_conflicts;
neighbors.at(siz-1)->f=neighbors.at(siz-1)->h+neighbors.at(siz-1)->g;
}
}
construct(Node* Node) //Display Solution
{
if(Node->parent!=NULL)
{
construct(Node->parent);
}
if(Node->parent!=NULL)
{
if(Node->parent->x0<Node->x0)
cout<<"Left";
else if(Node->parent->x0>Node->x0)
cout<<"Right";
else if(Node->parent->y0<Node->y0)
cout<<"Up";
else
cout<<"Down";
cout<<endl;
}
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
cout<<Node->Grid[i][j]<<"\t";
}
cout<<endl<<endl;
}
system("pause");
cout<<endl;
}
astar() //Generalized Algorithm
{
while(!open_list.empty())
{
process=*open_list_dup.begin(); //Node with min(f)
if(process->h==0) //Node found
{
tEnd=clock()-tStart;
return construct(process);
}
else
{
open_list_dup.erase(open_list_dup.begin());
open_list.erase(process); //Remove from nodes to be checked
closed_list.insert(process); //Insert in checked nodes
Neighbors(process); //Vector of next possible moves b/w 2-4
for(auto temp_Node:neighbors)
{
if(closed_list.count(temp_Node)==0)
{
open_list_cpy=open_list.find(temp_Node); //Iterator to node in 'to be checked nodes' same as new node
if(open_list_cpy==open_list.end()) //If new node not already in 'to be checked nodes'
{
open_list.insert(temp_Node); //Insert in both open list's
open_list_dup.insert(temp_Node);
}
else
{
if(temp_Node->g<(*open_list_cpy)->g) //More efficient parent found(new node)
{
f=temp_Node->f; //Reprioritizing/refresh/update priority queue
temp_Node->f=(*open_list_cpy)->f; //simulating set
open_list_dup.erase(temp_Node); //
temp_Node->f=f; //
open_list_dup.insert(temp_Node); //
(*open_list_cpy)->g=temp_Node->g; //changing the f,g and parent values
(*open_list_cpy)->f=temp_Node->f; //of original node.
(*open_list_cpy)->parent=temp_Node->parent; //
}
}
}
}
}
}
cout<<"No Solution"; //All Nodes to be checked are exhausted
}
int main()
{
linear_horizontal_conflicts=0;linear_vertical_conflicts=0;
tStart=clock();
neighbors.reserve(4);
for(i=0;i<N;i++) //Start Recognition and filling 'start' members.
{
maxi=-1;
maxj=-1;
for(j=0;j<N;j++)
{
if(grid[i][j]!=0&&(grid[i][j]-1)/N==i)
if(grid[i][j]>maxi)
maxi=grid[i][j];
else
linear_horizontal_conflicts++;
if(grid[j][i]!=0&&(grid[j][i]-1)%N==i)
if(grid[j][i]>maxj)
maxj=grid[j][i];
else
linear_vertical_conflicts++;
if(grid[i][j]==0)
{
start.x0=j+1;
start.y0=i+1;
}
else
start.manhattan_h+=abs(i-(grid[i][j]-1)/N)+abs(j-(grid[i][j]-1)%N);
start.Grid[i][j]=grid[i][j];
}
}
start.h=start.manhattan_h+2*linear_horizontal_conflicts+2*linear_vertical_conflicts;
start.g=0; //Initial 0th move
start.f=start.g+start.h; //f=g+h
open_list.insert(&start); //Insert start in both lists
open_list_dup.insert(&start);
astar();
cout<<"Moves Taken:\t"<<process->g<<endl; //Run Algorithm
cout<<"Time Taken:\t"<<(double)tEnd/CLOCKS_PER_SEC<<endl;
}