【优先队列广搜+前驱记录】HDU 1026 Ignatius and the Princess I

[url]http://acm.hdu.edu.cn/showproblem.php?pid=1026[/url]
[b][size=medium]
题意:问从图的左上角到达右下角需要的最短时间,如果格子是数字n(1-9),说明有怪兽,要打死他花费n的时间

[color=blue]Sample Input[/color]
[img]http://dl.iteye.com/upload/attachment/540847/127fd4bb-5aab-3153-9f9f-8908897e47d7.png[/img]

[color=blue]Sample Output[/color]
It takes 13 seconds to reach the target position, let me show you the way.
1s:(0,0)->(1,0)
2s:(1,0)->(1,1)
3s:(1,1)->(2,1)
4s:(2,1)->(2,2)
5s:(2,2)->(2,3)
6s:(2,3)->(1,3)
7s:(1,3)->(1,4)
8s:FIGHT AT (1,4)
9s:FIGHT AT (1,4)
10s:(1,4)->(1,5)
11s:(1,5)->(2,5)
12s:(2,5)->(3,5)
13s:(3,5)->(4,5)
[color=red]FINISH[/color]
It takes 14 seconds to reach the target position, let me show you the way.
1s:(0,0)->(1,0)
2s:(1,0)->(1,1)
3s:(1,1)->(2,1)
4s:(2,1)->(2,2)
5s:(2,2)->(2,3)
6s:(2,3)->(1,3)
7s:(1,3)->(1,4)
8s:FIGHT AT (1,4)
9s:FIGHT AT (1,4)
10s:(1,4)->(1,5)
11s:(1,5)->(2,5)
12s:(2,5)->(3,5)
13s:(3,5)->(4,5)
14s:FIGHT AT (4,5)
[color=red]FINISH[/color]
God please help our poor hero.
[color=red]FINISH[/color]
速度还不错:
[img]http://dl.iteye.com/upload/attachment/591354/ae3d709c-6a28-3726-8290-b5c1ae5f3998.png[/img][/size][/b]

#include <iostream>
#include <queue>
using namespace std;
#define M 105

struct point{
int x;
int y;
int times;
friend bool operator < (point a, point b)
{
return a.times > b.times; //重载小于号使得小的先出队列
}
};

struct Pre{
int px, py;
}pre[M][M];

int r, c;
int x_move[4] = {-1, 0, 1, 0};
int y_move[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;
point 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<point> 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 + x_move[i];
if (tp.x < 0 || tp.x >= r)
continue;
tp.y = ft.y + y_move[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;
/**********记录tp的前驱ft**********/
pre[tp.x][tp.y].px = ft.x;
pre[tp.x][tp.y].py = ft.y;
/**********记录tp的前驱ft**********/
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", map[i]);
memset (vis, false, sizeof(vis));
bfs (r-1, c-1); //倒过来搜索就可以不用栈了
puts ("FINISH");
}
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值