http://acm.hdu.edu.cn/showproblem.php?pid=1026

题意:给出一个迷宫,求出到终点的最短时间路径。

这道题目在迷宫上有怪物,不同HP的怪物会损耗不同的时间,这个时候可以用优先队列,每次让时间最短的出队列。由于最后还需要输出路径,所以需要设置一个数组来保存路径。

 #include<iostream>
#include<queue>
#include<cstring>
using namespace std; const int maxn = + ; struct node
{
int x, y;
int time;
friend bool operator < ( node a, node b) //重载<号
{
return b.time<a.time;
}
}; char map[maxn][maxn];
int visited[maxn][maxn];
int path[maxn][maxn];
int n, m;
int d[][] = { { , }, { -, }, { , }, { , - } }; int bfs()
{
node q,now;
priority_queue<node> p;
q.x = ;
q.y = ;
q.time = ;
p.push(q);
while (!p.empty())
{
q = p.top();
p.pop();
if (q.x == n - && q.y == m - ) return q.time;
for (int i = ; i < ; i++)
{
int xx = q.x + d[i][];
int yy = q.y + d[i][];
now.x = xx;
now.y = yy;
now.time = q.time;
if (xx >= && xx < n && yy >= && yy < m && map[xx][yy]!='X' && !visited[xx][yy])
{
if (map[xx][yy] == '.') now.time++;
else now.time =now.time+ (map[xx][yy] - ''+);
visited[xx][yy] = ;
path[xx][yy] = i + ; //记录路径
p.push(now);
}
}
}
return -;
} int temp; void print(int x, int y)
{
int xx, yy;
if (path[x][y] == ) return;
xx = x - d[path[x][y] - ][]; //寻找第一个路径点
yy = y - d[path[x][y] - ][];
print(xx, yy);
printf("%ds:(%d,%d)->(%d,%d)\n", temp++, xx, yy, x, y);
if (map[x][y] <= '' && map[x][y] >= '')
{
int m = map[x][y] - '';
while (m--) printf("%ds:FIGHT AT (%d,%d)\n", temp++, x, y);
}
} int main()
{
while (cin >> n >> m && (n||m))
{
memset(visited, , sizeof(visited));
memset(path, , sizeof(path));
for (int i = ; i < n;i++)
for (int j = ; j < m; j++)
cin >> map[i][j];
visited[][] = ;
int ans=bfs();
if (ans == -) cout << "God please help our poor hero." << endl;
else
{
cout << "It takes " << ans << " seconds to reach the target position, let me show you the way." << endl;
temp = ;
print(n - ,m - );
}
cout << "FINISH" << endl;
}
return ;
}

HDU 1026 Ignatius and the Princess I(带路径的BFS)的更多相关文章

  1. hdu 1026 Ignatius and the Princess I

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1026 Ignatius and the Princess I Description The Prin ...

  2. hdu 1026 Ignatius and the Princess I(BFS+优先队列)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1026 Ignatius and the Princess I Time Limit: 2000/100 ...

  3. hdu 1026 Ignatius and the Princess I (bfs+记录路径)(priority_queue)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1026 Problem Description The Princess has been abducted ...

  4. hdu 1026 Ignatius and the Princess I【优先队列+BFS】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  5. HDU 1026 Ignatius and the Princess I (广搜)

    题目链接 Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius ...

  6. HDU 1026 Ignatius and the Princess I(BFS+优先队列)

    Ignatius and the Princess I Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &am ...

  7. hdu 1026 Ignatius and the Princess I 搜索,输出路径

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  8. hdu 1026:Ignatius and the Princess I(优先队列 + bfs广搜。ps:广搜AC,深搜超时,求助攻!)

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  9. HDU 1026 Ignatius and the Princess I(BFS+记录路径)

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  10. hdu 1026 Ignatius and the Princess I(bfs)

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

随机推荐

  1. 使用Volley执行网络数据传输

    首先需要实例化一个RequestQueue RequestQueue queue = Volley.newRequestQueue(this); 然后是根据提供的URL请求字符串响应 String u ...

  2. Glide 小知识点

  3. 支付宝集成后报错ALI38173

    原因: 支付时传的参数不正确. 出现这个错误, 说明支付功能已经集成成功, 前后台核对下参数就能找到原因了.

  4. 初学者的python学习笔记1——作业篇

    既然是学习,作业必不可少,其实在看后面讲思路之前还是感觉自己写的不错,但是和后面一对比,感觉实在是想的太片面太肤浅了,还需要太多太多改进的地方. 首先放一下作业要求. 最开始做的时候真的是完全按照字面 ...

  5. Amoeba for MySQL读写分离配置

    一.      安装和运行Amoeba 1. Amoeba for MySQL 架构: 2. 验证Java的安装Amoeba框架是基于Java SE1.5开发的,建议使用Java SE1.5以上的版本 ...

  6. 从UWP到SWIFT - TableBarController 和 Pivot

    现在我还不是特别能适应swift中页面做bar的做法,感觉很奇怪. 现在我正在做一个简单的新浪微博,有一个主页,顶部有导航栏,底部是选项卡. 如果用wup来做的话,顶部应该是我们自己写的Usercon ...

  7. MAC按键以及快捷键

    使用普通的非Mac自带的键盘的同志们,想要在Mini Mac上面想要使用键盘,则推荐使用Mac系统自带的虚拟键盘,这样就可以查看普通键盘上每个键对应的Mac系统上是什么. 查看Mac系统上的虚拟键盘的 ...

  8. linux命令:df

    1.命令介绍: df用来检测磁盘空间占用情况. 2.命令格式: df [选项] 文件 3.命令参数: 必要参数: -a 全部文件系统列表 -h 方便阅读方式显示 -H 等于“-h”,但是计算式,1K= ...

  9. 【.net部署】Server Error in '/' Application.错误解决方案

    报错: Server Error in '/' Application.---------------------------------------------------------------- ...

  10. Python基于websocket实时通信的实现—GoEasy

    Python websocket实时消息推送 在这里我记录一下之前如何实现服务器端与客户端实时通信: 实现步骤如下: 1.        获取GoEasy appkey. 在goeasy官网上注册一个 ...