fix heuristic in astar.cpp

This commit is contained in:
Pavel 2021-11-09 10:05:06 +01:00
parent cab94daf65
commit 1c03f7b10c
1 changed files with 19 additions and 10 deletions

View File

@ -19,6 +19,12 @@ using namespace std;
int deltas[4] = {-16, 16, -1, 1};
union bytes
{
int i;
char b[4];
};
struct result
{
double cost;
@ -27,35 +33,38 @@ struct result
struct result cost(const vector<double> &t, int players, int exits, int m, int l)
{
int i, n, dn, p, e, mask;
int i, n, dn;
double c, v;
union bytes p, e;
struct result r;
p = players;
p.i = players;
e.i = exits;
dn = deltas[m];
c = 1;
for(i = 0; i < 4; ++i)
{
n = p >> i * 8 & 0xff;
e = exits >> i * 8 & 0xff;
if(n == e) continue;
n = p.b[i];
if(n == e.b[i]) continue;
n += dn;
v = t[n + i * l];
if(v < 0) continue;
mask = 0xff << i * 8;
p = (p & ~mask) | (n << i * 8);
p.b[i] = n;
c += v;
}
r.cost = c;
r.players = p;
r.players = p.i;
return r;
}
double heuristic(int u, int d)
{
int result, dx[4], i;
int dx[4], i;
union bytes a, b;
a.i = u;
b.i = d;
for(i = 0; i < 4; ++i)
{
dx[i] = abs(u >> i * 8 & 0xff) % 16 - abs(d >> i * 8 & 0xff) % 16 ;
dx[i] = abs(a.b[i] % 16 - b.b[i] % 16);
}
return MAX(dx[0], dx[2]) + MAX(dx[1], dx[3]);
}