HDU 1026 Ignatius and the Princess I(带路径的BFS)
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)的更多相关文章
- hdu 1026 Ignatius and the Princess I
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1026 Ignatius and the Princess I Description The Prin ...
- 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 ...
- 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 ...
- 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 ...
- HDU 1026 Ignatius and the Princess I (广搜)
题目链接 Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius ...
- HDU 1026 Ignatius and the Princess I(BFS+优先队列)
Ignatius and the Princess I Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d &am ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- Ajax读取txt并对txt内容进行分页显示
function TransferString(content) { var string = content; try{ string=string.replace(/\r\n/g,"&l ...
- xcode6 使用pch出错解决办法
1down vote If you decide to add a .pch file manually and you want to use Objective-C just like befor ...
- 有关javascript的性能优化(合理的管理内存)
使用具备垃圾收集机制的语言编写程序,开发人员一般不必操心内存管理的问题.但是,Javascript在进行内存管理及收集时面临的问题是有点与众不同.其中最主要的一个问题是分配给Web浏览器的可用内存数量 ...
- css3的3D和2D
css3的3D旋转:rorateX():参数为正值时,盒子是围绕x轴,完成从Y轴正方向到Y轴负方向的旋转,视觉上呈现高度上的变化.rorateY():参数为正值时,盒子是围绕Y轴,完成从X轴正方向到X ...
- 关于XML文档
很多人觉得XML很简单,但我想说其实一点也不简单,特别是在当今被各种web文档充斥的世界里有必要对XML进行一定程度的研究.下面的几篇博客将对XML技术进行简短的说明.
- Python学习路程day15
Python之路[第十五篇]:Web框架 Web框架本质 众所周知,对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. #!/usr/bin/en ...
- hdu 2074
ps:WA了好多次...因为首先是n=1的情况,其次是中心花色和外花色,中心花色也有可能是最外层花色....很无语. 然后就是格式问题咯. 代码 #include "stdio.h" ...
- Spring中的IOC\DI\AOP等概念的简单学习
IoC(Inversion of Control,控制反转).这是spring的核心,贯穿始终, 所谓IoC,对于spring框架来说,就是由spring来负责控制对象的生命周期和对象间的关系.Spr ...
- UE4 创建进程,打开额外程序 方法 笔记
FPlatformProcess::CreateProc(TEXT("程序路径");
- CentOS7 下 安装 supervisor以及使用
CentOS7 下 安装 supervisor 以及使用 手动安装 [注] linux环境必须安装 python 1.获取supervisor包:[https://pypi.python.org/py ...