Point Dijkstra(short r0, short r1, short c0, short c1, MapCell (*map)[MAP_WIDTH + 2], short att, short &distance, short (*dir)[2])
{
short dist[MAP_HEIGHT + 2][MAP_WIDTH + 2];
fill(dist[0], dist[0] + (MAP_HEIGHT + 2) * (MAP_WIDTH + 2), 999);
dist[r0][c0] = 0;
short parent[MAP_HEIGHT + 2][MAP_WIDTH + 2];
parent[r0][c0] = -1;
short r, c;
list<Point> node_list;
Point pt;
for (short i=1; i<=MAP_HEIGHT; ++i)
{
for (short k=1; k<=MAP_WIDTH; ++k)
{
if (map[i][k].type == STONE)
continue;
pt.row = i;
pt.col = k;
node_list.push_front(pt);
}
}
while (!node_list.empty())
{
//get min node
list<Point>::iterator i = node_list.begin(), m = node_list.begin();
for (; i != node_list.end(); ++i)
{
if (dist[i->row][i->col] < dist[m->row][m->col])
m = i;
}
pt = *m;
node_list.erase(m);
//relax each adjacent node
for (short k=0; k<4; ++k)
{
r = pt.row + dir[k][ROW];
c = pt.col + dir[k][COL];
if (r <= 0 || r > MAP_HEIGHT || c <= 0 || c > MAP_WIDTH || map[r][c].type == STONE)
continue;
short t;
if (map[r][c].type == PERVIOUS) t = 0;
else if (map[r][c].type = BRICK) t = 2 / att;
else t = 1;
if (dist[r][c] > dist[pt.row][pt.col] + t + 1)
{
dist[r][c] = dist[pt.row][pt.col] + t + 1;
parent[r][c] = k;
}
}
}
distance = dist[r1][c1];
r = r1;
c = c1;
short k = -1;
while (parent[r][c] != -1)
{
assert(r >= 0 && r <= MAP_HEIGHT + 1 && c >= 0 && c <= MAP_WIDTH + 1);
k = parent[r][c];
r -= dir[k][ROW];
c -= dir[k][COL];
}
if (k == -1)
r = c = -1;
else
{
r += dir[k][ROW];
c += dir[k][COL];
}
pt.row = r;
pt.col = c;
return pt;
}