#include <iostream>
#include <queue>
using namespace std;
#define M 105
struct ppp {
int x;
int y;
int times;
friend bool operator < (ppp a, ppp b)
{
return a.times > b.times;
}
};
struct Pre {
int px, py;
}pre[M][M];
int r, c;
int gox[4] = { -1, 0, 1, 0 };
int goy[4] = { 0, 1, 0, -1 };
char map[M][M];
bool vis[M][M];
void bfs(int x, int y)
{
int i, v;
vis[x][y] = true;
ppp ft, tp;
pre[x][y].px = -1;
ft.x = x, ft.y = y, ft.times = 0;
if (map[x][y] != '.')
ft.times = map[x][y] - '0';
priority_queue<ppp> q;
q.push(ft);
while (!q.empty())
{
ft = q.top();
q.pop();
if (ft.x == 0 && ft.y == 0)
{
printf("It takes %d seconds to reach the target position, let me show you the way.\n", ft.times);
int key = 1, total = ft.times;
x = ft.x, y = ft.y;
while (pre[x][y].px != -1)
{
int tx = pre[x][y].px;
int ty = pre[x][y].py;
printf("%ds:(%d,%d)->(%d,%d)\n", key++, x, y, tx, ty);
if (map[tx][ty] != '.')
for (i = 0; i < map[tx][ty] - '0'; i++)
printf("%ds:FIGHT AT (%d,%d)\n", key++, tx, ty);
x = tx;
y = ty;
}
return;
}
for (i = 0; i < 4; i++)
{
tp.x = ft.x + gox[i];
if (tp.x < 0 || tp.x >= r)
continue;
tp.y = ft.y + goy[i];
if (tp.y < 0 || tp.y >= c)
continue;
if (vis[tp.x][tp.y])
continue;
vis[tp.x][tp.y] = true;
if (map[tp.x][tp.y] == 'X')
continue;
if (map[tp.x][tp.y] == '.')
v = 1;
else v = map[tp.x][tp.y] - '0' + 1;
tp.times = ft.times + v;
pre[tp.x][tp.y].px = ft.x;
pre[tp.x][tp.y].py = ft.y;
q.push(tp);
}
}
puts("God please help our poor hero.");
}
int main()
{
int i;
while (~scanf("%d%d", &r, &c))
{
for (i = 0; i < r; i++)
scanf_s("%s", map[i]);
memset(vis, false, sizeof(vis));
bfs(r - 1, c - 1);
puts("FINISH");
}
return 0;
}
用bfs,然后找出能抵达终点的最短时间,途中打怪兽的时候,处理为停止不动,打几秒就停几秒,别的方向该怎么走就怎么走。这是一个不同的地方,想到就好说。途中还要记录路径,最后输出最短的。