-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
459 lines (440 loc) · 10.4 KB
/
main.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
// ordered priority queue using binary heap
// array index start from 1
#include <bits/stdc++.h>
using namespace std;
// this class will containg each 3x3 grid with corresponding costs.
class board
{
public:
int arr[3][3],manhattan;
// dist function will give the manhattan distance between the current bord and goal.
int dist(int arr[3][3])
{
int sum = 0;
int ans[3][3] = {{1,2,3},
{4,5,6},
{7,8,0}};
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(arr[i][j]==1)
{
sum+=(i+j);
}
else if(arr[i][j]==2)
{
sum+=(i+abs(j-1));
}
else if(arr[i][j]==3)
{
sum+=(i+abs(j-2));
}
else if(arr[i][j]==4)
{
sum+=(abs(i-1)+j);
}
else if(arr[i][j]==5)
{
sum+=(abs(i-1)+abs(j-1));
}
else if(arr[i][j]==6)
{
sum+=(abs(i-1)+abs(j-2));
}
else if(arr[i][j]==7)
{
sum+=(abs(i-2)+j);
}
else if(arr[i][j]==8)
{
sum+=(abs(i-2)+abs(j-1));
}
}
}
return sum;
}
// custom constructors
board()
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
arr[i][j]=0;
}
}
manhattan = 0;
}
board(int arr1[3][3])
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
arr[i][j]=arr1[i][j];
}
}
manhattan = dist(arr1);
}
// overriting the equal to operator for the board class.
bool operator==(const board& that)
{
if(manhattan == that.manhattan)
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(arr[i][j] != that.arr[i][j])
return false;
}
}
return true;
}
return false;
}
// custom assignment operator
board & operator=(const board& that)
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
arr[i][j]=that.arr[i][j];
}
}
manhattan = that.manhattan;
return *this;
}
// custom copy constructor
board(board & that)
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
arr[i][j]=that.arr[i][j];
}
}
manhattan = that.manhattan;
}
// prints the current board elements.
void print()
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<arr[i][j]<<" ";
}
cout<<"\n";
}
}
};
template<class T,class compare>
class ordered_pq
{
private:
T* key;
int capacity;
void exch(int i,int j);
void increase();
void swim(int k); // child key become larger than its parents key
void sink(int k); // parents key become smaller than one or both of its children
// exchange that key with the max of children
public:
int N;
ordered_pq();
ordered_pq(int length);
bool isEmpty();
void insert(T item); // insert item at the end and swim it up
T delMin();
void print();
};
template<class T,class compare>
void ordered_pq<T,compare>::sink(int k)
{
compare cmp;
while(2*k <= N)
{
int j = 2*k;
if(j < N && cmp(key[j],key[j+1]))
j++;
if(cmp(key[k],key[j]))
exch(k,j);
else
break;
k = j;
}
}
template<class T,class compare>
void ordered_pq<T,compare>::swim(int k)
{
compare cmp;
while(k>1 && cmp(key[k/2],key[k]))
{
exch(k,k/2);
k = k/2;
}
}
template<class T,class compare>
ordered_pq<T,compare>::ordered_pq()
{
capacity = 2;
N=0;
key = new T[capacity+1];
}
template<class T,class compare>
ordered_pq<T,compare>::ordered_pq(int length)
{
capacity = length;
N=0;
key = new T[capacity+1];
}
template<class T,class compare>
void ordered_pq<T,compare>::exch(int i,int j)
{
T temp = key[i];
key[i] = key[j];
key[j] = temp;
}
template<class T,class compare>
bool ordered_pq<T,compare>::isEmpty()
{
if(N)
return false;
return true;
}
template<class T,class compare>
void ordered_pq<T,compare>::insert(T item)
{
if(N == capacity)
increase();
key[++N] = item;
swim(N);
}
template<class T,class compare>
T ordered_pq<T,compare>::delMin()
{
exch(1,N--);
sink(1);
return key[N+1];
}
template<class T,class compare>
void ordered_pq<T,compare>::increase()
{
capacity *= 2;
T* newkey = new T[capacity+1];
for(int i=1;i<=N;i++)
{
newkey[i] = key[i];
}
delete[] key;
key = newkey;
}
// container will contain an array of board which will be in ascending order
// in which the indexes should be moved
class container
{
public:
container();
board* store;
int priority,N,capacity,moves,manhattan;
void insert(board b);
board get();
void increase();
void print();
void remove();
// custom assignment operator
container & operator=(const container& that)
{
priority = that.priority;
moves = that.moves;
manhattan = that.manhattan;
N = that.N;
capacity = that.capacity;
store = new board[capacity+1];
for(int i=0;i<N;i++)
{
store[i] = that.store[i];
}
return *this;
}
// custom copy constructor
container(container & that)
{
priority = that.priority;
moves = that.moves;
manhattan = that.manhattan;
N = that.N;
capacity = that.capacity;
store = new board[capacity+1];
for(int i=0;i<N;i++)
{
store[i] = that.store[i];
}
}
};
container::container()
{
priority = INT_MAX;
moves = 0;
manhattan = INT_MAX;
capacity = 2;
N = 0;
store = new board[capacity+1];
}
void container::increase()
{
capacity = capacity*2;
board* temp = new board[capacity+1];
for(int i=0;i<N;i++)
{
temp[i] = store[i];
}
delete[] store;
store = temp;
}
void container::insert(board b)
{
if(N>=capacity)
increase();
store[N++] = b;
moves++;
manhattan = b.manhattan;
priority = moves+manhattan;
}
board container::get()
{
return store[N-1];
}
void container::print()
{
for(int i=0;i<N;i++)
{
store[i].print();
cout<<"\n";
}
}
void container::remove()
{
moves--;
manhattan = store[--N].manhattan;
priority = moves+manhattan;
}
class comp1
{
public:
bool operator() (container* c1,container* c2)
{
if(c1->priority>c2->priority)
{
return true;
}
if(c1->priority<c2->priority)
{
return false;
}
return c1->manhattan>c2->manhattan;
}
};
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
freopen("output.txt","w",stdout);
ordered_pq<container*,comp1> game;
int arr[3][3];
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cin>>arr[i][j];
}
}
container* cont = new container;
board b1(arr);
cont->insert(b1);
game.insert(cont);
while(b1.manhattan)
{
cont = game.delMin();
while(!game.isEmpty())
{
container* cont2 = game.delMin();
if(!(cont2->get() == cont->get()))
{
game.insert(cont2);
break;
}
}
b1 = cont->get();
int k,l;
bool flag=false;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(b1.arr[i][j] == 0)
{
k=i;
l=j;
flag=true;
break;
}
}
if(flag)
break;
}
if(k-1 >= 0)
{
container* cont1 = new container((*cont));
b1.arr[k][l] = b1.arr[k-1][l];
b1.arr[k-1][l] = 0;
board temp(b1.arr);
cont1->insert(temp);
game.insert(cont1);
b1.arr[k-1][l] = b1.arr[k][l];
b1.arr[k][l] = 0;
}
if(k+1 <= 2)
{
container* cont1 = new container((*cont));
b1.arr[k][l] = b1.arr[k+1][l];
b1.arr[k+1][l] = 0;
board temp(b1.arr);
cont1->insert(temp);
game.insert(cont1);
b1.arr[k+1][l] = b1.arr[k][l];
b1.arr[k][l] = 0;
}
if(l-1 >= 0)
{
container* cont1 = new container((*cont));
b1.arr[k][l] = b1.arr[k][l-1];
b1.arr[k][l-1] = 0;
board temp(b1.arr);
cont1->insert(temp);
game.insert(cont1);
b1.arr[k][l-1] = b1.arr[k][l];
b1.arr[k][l] = 0;
}
if(l+1 <= 2)
{
container* cont1 = new container((*cont));
b1.arr[k][l] = b1.arr[k][l+1];
b1.arr[k][l+1] = 0;
board temp(b1.arr);
cont1->insert(temp);
game.insert(cont1);
b1.arr[k][l+1] = b1.arr[k][l];
b1.arr[k][l] = 0;
}
}
cout<<"ANS\n";
cout<<game.N<<"\n";
cout<<"priority = "<<cont->priority<<", manhattan = "<<cont->manhattan<<", moves ="<<cont->moves<<"\n";
cont->print();
return 0;
}