-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path25p.cpp
More file actions
309 lines (284 loc) · 6.41 KB
/
Copy path25p.cpp
File metadata and controls
309 lines (284 loc) · 6.41 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
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
#include <iostream>
#include <ctime>
#include <queue>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <random>
#include <bits/stdc++.h>
#include <chrono>
using namespace std;
// typedefs for unsigned types
typedef unsigned int ui;
typedef unsigned char uc;
// Global vars
bool isSolved = false;
int Size;
char mdTable[25][25];
char movableTable[25][4];
uc path[256];
ui nodeCount;
char dirs[4] = {-1, 1, -2, 2};
char moves[24][4];
struct NodeInfo {
ui id;
ui parent;
char blank;
char move;
};
struct Node {
char board[25];
char move;
NodeInfo nodeInfo;
uc f, g;
Node(char* bd, char blk, char move, ui id, ui parent, uc f = 0, uc g = 0){
for (int i = 0; i < Size * Size; ++i) {
board[i] = bd[i];
}
nodeInfo.blank = blk;
nodeInfo.id = id;
nodeInfo.parent = parent;
nodeInfo.move = move;
this->f = f;
this->g = g;
}
bool operator >(const Node& other) const{
return f > other.f;
}
};
void MakeDirections(){
int i = 0;
char dirs[] = {0, 1, 2, 3};
do{
moves[i][0] = dirs[0];
moves[i][1] = dirs[1];
moves[i][2] = dirs[2];
moves[i++][3] = dirs[3];
}while ( next_permutation(dirs, dirs + 4) );
}
void MakeMovableTable(int size){
int moves[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int board[5][5];
for(int y = 0; y < size; ++y){
for(int x = 0; x < size; ++x){
board[y][x] = x + y * size;
}
}
int dx, dy, j;
for(int y = 0; y < size; ++y){
for(int x = 0; x < size; ++x){
for(int i = 0; i < 4; ++i){
dx = moves[i][0];
dy = moves[i][1];
if(x + dx < 0 || y + dy < 0 || x + dx >= size || y + dy >= size){
j = -1;
}
else{
j = board[y + dy][x + dx];
}
movableTable[x + y * size][i] = j;
}
}
}
}
void MakeMDTable(int size){
for(int y = 0, i = 1; y < size * size; ++y, (++i) % (size * size)){
for(int x = 0; x < size * size; ++x){
if(i == 0){
mdTable[i][x] = 0;
}
else{
mdTable[i][x] = abs((y / size) - (x / size)) + abs((y % size) - (x % size));
}
}
}
}
char GetDistance(int *row, char md, int nums){
if (nums > 1){
if (nums == 2){
if (row[0] > row[1])
md += 2;
}
else if (nums == 3){
if (row[0] > row[1] || row[0] > row[2])
md += 2;
if (row[1] > row[2])
md += 2;
}
else if (nums == 4){
if (row[0] > row[1] || row[0] > row[2] || row[0] > row[3])
md += 2;
if (row[1] > row[2] || row[1] > row[3])
md += 2;
if (row[2] > row[3])
md += 2;
}
else if (nums == 5){
if (row[0] > row[1] || row[0] > row[2] || row[0] > row[3] || row[0] > row[4])
md += 2;
if (row[1] > row[2] || row[1] > row[3] || row[1] > row[4])
md += 2;
if (row[2] > row[3] || row[2] > row[4])
md += 2;
if(row[3] > row[4])
md += 2;
}
}
return md;
}
char GetHeuristic(char* puzzle){
int i, j, x, md = 0;
int k, n;
int temp[5];
for(i = 0; i < Size * Size; ++i){
md += mdTable[puzzle[i]][i];
}
for (i = 0, x = 1; i < Size; ++i, ++x){
k = 0;
for (j = 0; j < Size; ++j)
{
n = puzzle[i * Size + j];
if (n <= Size * x && n > Size * (x - 1))
{
temp[k++] = n;
}
}
md = GetDistance(temp, md, k);
}
for (i = 0, x = 1; i < Size; ++i, ++x){
k = 0;
for (j = 0; j < Size; ++j){
n = puzzle[j * Size + i];
if (n == x || n == x + Size || n == x + Size * 2 || n == x + Size * 3){
temp[k++] = n;
}
}
md = GetDistance(temp, md, k);
}
return md;
}
char GetBlank(char *puzzle){
for(int i = 0; i < Size * Size; ++i){
if (puzzle[i] == 0){
return i;
}
}
}
void PrintPath(char depth){
for(int i = 0; i < depth; ++i){
printf("%2d ",path[i]);
if((i + 1) % 10 == 0)
printf("\n");
}
printf("\n\n");
}
void PrintPuzzle(char* puzzle){
for(int i = 0; i < Size; ++i){
for(int j = 0; j < Size; ++j){
printf("%2d ", puzzle[i * Size + j]);
}
printf("\n");
}
printf("\n");
}
int i = 0;
void IdaStar(char maxDepth, char* puzzle, char lastMove, char blank, char dir){
if (maxDepth == 0){
isSolved = true;
return;
}
else{
for (int j = 0; j < 4; ++j){
if(lastMove == -1 || (dirs[moves[dir][j]] + dirs[lastMove]) != 0){
char newBlank = movableTable[blank][moves[dir][j]];
if(newBlank == -1)
continue;
puzzle[blank] = puzzle[newBlank];
puzzle[newBlank] = 0;
uc h = GetHeuristic(puzzle);
if(h < maxDepth && !isSolved){
nodeCount += 1;
path[i++] = puzzle[blank];
IdaStar(maxDepth - 1, puzzle, moves[dir][j], newBlank, dir);
--i;
}
puzzle[newBlank] = puzzle[blank];
puzzle[blank] = 0;
}
}
}
}
void IDAStar(char* puzzle, char dir){
time_t start, end;
uc h, depth;
char lastMove = -1;
char blank = GetBlank(puzzle);
isSolved = false;
ui totalCount = 0;
h = GetHeuristic(puzzle);
depth = h;
start = clock();
while (true){
nodeCount = 0;
IdaStar(depth, puzzle, lastMove, blank, dir);
end = clock();
totalCount += nodeCount;
printf("Depth = %d, Node Count = %10u, Total Count = %10u, time = %6.2f seconds\n",depth, nodeCount, totalCount, (end - start) / 1000.);
if (isSolved){
printf("Total Node Count = %u, shortest path length = %d, time = %.2f seconds\n",totalCount, depth, (end - start) / 1000.);
return;
}
depth += 2;
}
}
void shuffle_array(char arr[], int n) { //from : https://www.geeksforgeeks.org/shuffle-an-array-using-stl-in-c/
// To obtain a time-based seed
unsigned seed = chrono::system_clock::now().time_since_epoch().count();
// Shuffling our array
shuffle(arr, arr + n, default_random_engine(seed));
}
int* toInt(char * c , int s){
int *board=new int[s];
for(int i=0;i<s;i++){
int a=(int) c[i];
board[i]=a;
}
return board;
}
bool solvable(char * c){
int *board=toInt(c,25);
// for(int i=0;i<16;i++)
// cout<<board[i]<<" ";
// cout<<endl;
int count=0;
for(int i=0;i<24;i++){
for (int j=i+1;j<25;j++)
if(board[i]>board[j] && board[i]!=0 &&board[j]!=0){
count++;
}
}
return (count%2+1)%2;
}
int main()
{
int size = 5;
char puzzle[25]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,0};
char p[25];
srand((ui)time(NULL));
char dir = rand() % 24;
MakeMDTable(size);
MakeMovableTable(size);
MakeDirections();
Size = size;
shuffle_array(puzzle,25);
while(!solvable(puzzle)){
shuffle_array(puzzle,25);
}
cout << " PUZZLE TO SOLVE \n--------------------" << endl << endl;
PrintPuzzle(puzzle);
cout << "--------------------" << endl << endl;
// cout<<solvable(puzzle)<<endl;
printf("\n===== IDA Star Algiorithm =====\n");
IDAStar(puzzle, dir);
return 0;
}