-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIDA*.cpp
217 lines (206 loc) · 4.15 KB
/
IDA*.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
#include <bits/stdc++.h>
//( (grid width odd) && (#inversions even) ) || ( (grid width even) && ((blank on odd row from bottom) == (#inversions even)) )
#define fwrite() freopen("out.txt","w",stdout)
using namespace std;
int grid[20],manh[20][20];
char path[50];
bool done;
int bound;
int man()
{
int i,j,r1,c1,r2,c2;
for(i=1;i<=16;i++)
{
r1=(i-1)/4;
r1++;
c1=i%4;
if(c1==0)
c1=4;
for(j=1;j<=16;j++)
{
r2=(j-1)/4;
r2++;
c2=j%4;
if(c2==0)
c2=4;
manh[i][j]=abs(r1-r2)+abs(c1-c2);
}
}
}
int h()
{
int i,k=0;
for(i=1;i<=16;i++)
{
if(grid[i]!=16)
k=k+manh[grid[i]][i];
}
return k;
}
bool solvable()
{
int i,j,k=0,blnk;
for(i=1;i<=16;i++)
{
if(grid[i]==16)
{
blnk=(i-1)/4;
blnk=4-blnk;
continue;
}
for(j=i+1;j<=16;j++)
{
if(grid[j]<grid[i])
k++;
}
}
if(k%2!=blnk%2)
return true;
return false;
}
int mx;
string s;
int solve(int g_val,int h_val,int blank,int mv)
{
int f_val=g_val+h_val;
if(h_val==0)
{
done=true;
if(f_val<mx)
{
mx=f_val;
int i=0;
s.clear();
while(i<f_val)
{
s.push_back(path[i]);
i++;
}
}
else if(f_val==mx)
{
string s2;
int i=0;
while(i<f_val)
{
s2.push_back(path[i]);
i++;
}
if(s2<s)
{
s.clear();
s=s2;
}
}
return f_val;
}
if(f_val>bound)
return f_val;
int i,j,k,mv1=1000,mv2=1000,mv3=1000,mv4=1000;
if(blank%4!=1&&mv!=3)
{
path[g_val]='L';
k=h_val-manh[blank-1][grid[blank-1]];
swap(grid[blank],grid[blank-1]);
k=k+manh[blank][grid[blank]];
mv1=solve(g_val+1,k,blank-1,1);
swap(grid[blank],grid[blank-1]);
}
if(blank>4&&mv!=4)
{
path[g_val]='U';
k=h_val-manh[blank-4][grid[blank-4]];
swap(grid[blank],grid[blank-4]);
k=k+manh[blank][grid[blank]];
mv2=solve(g_val+1,k,blank-4,2);
swap(grid[blank],grid[blank-4]);
}
if(blank%4&&mv!=1)
{
path[g_val]='R';
k=h_val-manh[blank+1][grid[blank+1]];
swap(grid[blank],grid[blank+1]);
k=k+manh[blank][grid[blank]];
mv3=solve(g_val+1,k,blank+1,3);
swap(grid[blank],grid[blank+1]);
}
if(blank<13&&mv!=2)
{
path[g_val]='D';
k=h_val-manh[blank+4][grid[blank+4]];
swap(grid[blank],grid[blank+4]);
k=k+manh[blank][grid[blank]];
mv4=solve(g_val+1,k,blank+4,4);
swap(grid[blank],grid[blank+4]);
}
if(mv4<=mv1&&mv4<=mv2&&mv4<=mv3)
{
path[g_val]='D';
return mv4;
}
else if(mv1<=mv2&&mv1<=mv3&&mv1<=mv4)
{
path[g_val]='L';
return mv1;
}
else if(mv3<=mv1&&mv3<=mv2&&mv3<=mv4)
{
path[g_val]='R';
return mv3;
}
else if(mv2<=mv1&&mv2<=mv3&&mv2<=mv4)
{
path[g_val]='U';
return mv2;
}
}
int ida(int pos)
{
bound=h();
int m=bound;
while(bound<=35&&!done)
{
bound=solve(0,m,pos,10);
}
return bound;
}
int main()
{
int i,j,k,m,n,cs=0;
man();
//fwrite();
cin>>n;
while(n--)
{
cs++;
for(i=1;i<=16;i++)
{
cin>>grid[i];
if(grid[i]==0)
{
grid[i]=16;
j=i;
}
}
cout<<"Case "<<cs<<": ";
if(solvable())
{
done=false;
s.clear();
mx=100000;
k=ida(j);
if(done&&k<=35)
{
cout<<s;
}
else
cout<<"This puzzle is not solvable.";
}
else
{
cout<<"This puzzle is not solvable.";
}
cout<<endl;
}
return 0;
}