-
Notifications
You must be signed in to change notification settings - Fork 2
/
IDA_star.cpp
233 lines (157 loc) · 4.59 KB
/
IDA_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
#include<bits/stdc++.h>
#include"solvability.h"
using namespace std;
typedef long long int lli;
#define mod 1000000007
#define F first
#define S second
#define pb push_back
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
lli power(lli x, int y){x=x%mod; if(x==0)return 0;lli res = 1;
while(y>0){ if(y%2) res = (res*x)%mod; y = y/2; x = (x*x)%mod;}return res;}
#define N 3
struct Node{
int **scramble;
pair<int,int> blank;
}puzzle;
int pos[N*N][2]; int Hash[N][N][N*N];
int moves[100];
int bound; int solved;
int direcX[]={1,-1,0,0};
int direcY[]={0,0,1,-1};
int heuristic(){
//manhattan heurestic
int res=0;
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
if(puzzle.scramble[i][j]!=0){
int val=puzzle.scramble[i][j];
res+=abs(i-pos[val][0])+abs(j-pos[val][1]);
}
}
}
return res;
}
bool safe_check(int x, int y){
return (x>=0 && x< N && y>=0 && y< N);
}
Node initial;
void printState(int **puzzle){
for(int i=0;i<N;i++){
for(int j=0;j<N;j++)
cout<<puzzle[i][j]<<" ";
cout<<"\n";
}
cout<<"\n";
}
void printpath(){
int i=0;
printState(initial.scramble);
int blankx=initial.blank.F;
int blanky=initial.blank.S;
while(moves[i]!=-1){
if(moves[i]==1){
swap(initial.scramble[blankx][blanky],initial.scramble[blankx-1][blanky]);
blankx--;
}
else if(moves[i]==0){
swap(initial.scramble[blankx][blanky],initial.scramble[blankx+1][blanky]);
blankx++;
}
else if(moves[i]==2){
swap(initial.scramble[blankx][blanky],initial.scramble[blankx][blanky+1]);
blanky++;
}
else{
swap(initial.scramble[blankx][blanky],initial.scramble[blankx][blanky-1]);
blanky--;
}
printState(initial.scramble);
i++;
}
}
int ida_star(int depth , int heu){
if(heu==0){
solved=1;
moves[depth]=-1;
printpath();
return depth;
}
if(depth + 5*heu/3 > bound)
return depth + 5*heu/3;
int nextbound=1e9; int val=1e9;
for(int i=0;i<4;i++){
int inX=puzzle.blank.F; int inY=puzzle.blank.S;
int X=inX+ direcX[i] ; int Y=inY + direcY[i];
int index=-1;
if(depth)
index=moves[depth-1];
if(safe_check(X,Y) && ((depth==0)|| (X!=(inX-direcX[index]) && Y!=(inY-direcY[index])))){
pair<int,int> newblank={X,Y};
int child_heu=heu;
child_heu-=Hash[X][Y][puzzle.scramble[X][Y]];
child_heu+=Hash[inX][inY][puzzle.scramble[X][Y]];
swap(puzzle.scramble[inX][inY],puzzle.scramble[X][Y]);
puzzle.blank=newblank;
moves[depth]=i;
val=ida_star(depth+1,child_heu);
swap(puzzle.scramble[inX][inY],puzzle.scramble[X][Y]);
puzzle.blank.F=inX;
puzzle.blank.S=inY;
if(solved)
return val;
nextbound=min(nextbound,val);
}
}
return nextbound;
}
int main(){
IOS;
puzzle.scramble=(int **)malloc(N*sizeof(int *));
for(int i=0;i<N;i++)
puzzle.scramble[i]=(int *)malloc(N*sizeof(int));
initial.scramble=(int **)malloc(N*sizeof(int *));
for(int i=0;i<N;i++)
initial.scramble[i]=(int *)malloc(N*sizeof(int));
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
cin>>puzzle.scramble[i][j];
initial.scramble[i][j]=puzzle.scramble[i][j];
if(puzzle.scramble[i][j]==0){
puzzle.blank={i,j};
initial.blank={i,j};
}
}
}
clock_t start,end;
start=clock();
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
if(!(i==N-1 && j==N-1)){
pos[i*N+j+1][0]=i;
pos[i*N+j+1][1]=j;
}
}
}
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
for(int k=1;k<N*N;k++){
Hash[i][j][k]=abs(i-pos[k][0])+abs(j-pos[k][1]);
}
}
}
if(isSolvable(puzzle.scramble,N)){
bound=heuristic(); solved=0;
int initial_heu=bound;
while(solved==0){
bound=ida_star(0,initial_heu);
}
}
else
cout<<"The given instance of the puzzle cannot be solved\n";
end=clock();
double time_taken = double(end - start) / double(CLOCKS_PER_SEC);
cout<<"Time taken in execution is : "<<time_taken<<" secs\n";
}