hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)
以前写的题了,现在想整理一下,就挂出来了。
题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1)。
'X'为墙,'.'为路,数字为怪物。墙不能走,路花1s经过,怪物需要花费1s+数字大小的时间。
比较麻烦的是需要记录路径。还要记录是在走路还是在打怪。
因为求最短路,所以可以使用bfs。
因为进过每一个点花费时间不同,所以可以使用优先队列。
因为需要记录路径,所以需要开一个数组,来记录经过节点的父节点。当然,记录方法不止一种。
上代码——
#include <cstdio>
#include <cstring>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std; struct node
{
int x, y, step;
bool operator < (const node& a) const
{
return a.step < step;
}
}; int go[][] = {{, },{-, }, {, }, {, -}}; int n, m, step;
bool v[][];
char mp[][];
int last[][]; bool bfs()
{
node p, q;
p.x = n-;
p.y = m-;
p.step = ;
if(mp[n-][m-] >= '' && mp[n-][m-] <= '') p.step += mp[n-][m-] -''; priority_queue <node> que;
if(mp[p.x][p.y] != 'X')
que.push(p);
v[p.x][p.y] = ; while(!que.empty())
{
p = que.top();
que.pop(); for(int i = ; i < ; i++)
{
int x = p.x+go[i][];
int y = p.y+go[i][]; if(x >= && x < n && y >= && y < m && !v[x][y])
{
if(mp[x][y] == 'X') continue; q.x = x; q.y = y; q.step = p.step+;
if(mp[x][y] >= '' && mp[x][y] <= '') q.step += mp[x][y]-'';
que.push(q);
v[x][y] = ;
last[x][y] = p.x*+p.y;
if(x == && y == ) {step = q.step; return ;} }
}
}
return ;
} void output()
{
printf("It takes %d seconds to reach the target position, let me show you the way.\n", step);
int x = ;
int y = ;
int i = ;
while(x != n- || y != m-)
{
if(mp[x][y] >= '' && mp[x][y] <= '')
{
int stop = mp[x][y] - '';
while(stop--)
{
printf("%ds:FIGHT AT (%d,%d)\n", i++, x, y);
}
}
printf("%ds:(%d,%d)->(%d,%d)\n", i++, x, y, last[x][y]/, last[x][y]%); int t = last[x][y]/;
y = last[x][y]%;
x = t;
}
if(mp[x][y] >= '' && mp[x][y] <= '')
{
int stop = mp[x][y] - '';
while(stop--)
{
printf("%ds:FIGHT AT (%d,%d)\n", i++, x, y);
}
}
} int main()
{
//freopen("test.txt", "r", stdin);
while(~scanf("%d%d", &n, &m))
{
memset(mp, , sizeof(mp));
memset(v, , sizeof(v));
memset(last, , sizeof(last));
for(int i = ; i < n; i++)
{
scanf("%s", mp[i]);
} if(bfs()) output();
else printf("God please help our poor hero.\n");
printf("FINISH\n");
}
return ;
}
hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)的更多相关文章
- 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(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)
题目链接 题意 : 从(0,0)点走到(N-1,M-1)点,问最少时间. 思路 : BFS..... #include <stdio.h> #include <string.h> ...
- 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
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1026 Ignatius and the Princess I Description The Prin ...
- HDU1026--Ignatius and the Princess I(BFS记录路径)
Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has ...
- 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(优先队列 + 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+记录路径)(priority_queue)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1026 Problem Description The Princess has been abducted ...
随机推荐
- SilverLight页面跳转(转载)
// Silverlight页面的跳转 // (Application.Current.RootVisualasIContent).Content=newDragControl(); //Silver ...
- Struts2 SSH整合框架返回json时,要注意懒加载问题
返回的这个json对象,要保证它里面的所有属性都已经取出来了(即不是proxy或者是懒加载),否则当struts框架将该对象转化成json数据时,会报出一个no session的错误. 因此你要将该懒 ...
- linux入门教程(九) 文本编辑工具vim
前面多次提到过vim这个东西,它是linux中必不可少的一个工具.没有它很多工作都无法完成.早期的Unix都是使用的vi作为系统默认的编辑器的.你也许会有疑问,vi与vim有什么区别?可以这样简单理解 ...
- BZOJ 3720 gty的妹子树
块状树裸题 块状树: 首先对树进行分块,分出的每一块都是一个连通块 通常的分块的方式如下: 1.父亲所在块不满,分到父亲所在块中 2.父亲所在块满,自己单独开一个块 (貌似有更为优越的分块方式? 注意 ...
- ubuntu 修改主机及主机名
修改主机: sudo vim /etc/hostname sudo vim /etc/hosts 修改用户名: sudo vim /etc/passwd sudo mv /home/yinggc /h ...
- avi 格式详解
http://blog.csdn.net/becomly/article/details/6283004 http://blog.csdn.net/easecom/article/details/45 ...
- Android:密码显示隐藏
效果: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android= ...
- C#四种文件流的区别(转)
1.FileStream类的读写操作 FileStream类可以对任意类型的文件进行读取操作,而且我们也可以按照需要指定每一次读取字节长度,以此减少内存的消耗,提高读取效率. 代码实例: //创建文件 ...
- NSMutableString
/*可变字符串,注意NSMutableString是NSString子类*/ //注意虽然initWithCapacity分配字符串大小,但是不是绝对的不可以超过此范围,声明此变量对 性能有好处 NS ...
- shell 编程基础(1)---初识shellscript
shellscript 是linux下强大的系统管理工具,可以通过bash命令和管道命令直接在linux系统上进行编程,所写的脚本不需要编译就可以执行,对于系统管理而言十分方便. #!/bin/bas ...