hdu-1026(bfs+优先队列)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1026
题意:输入n,m和一个n*m的矩阵,
.表示通路;
x表示墙;
n表示有一个怪物,消灭它需要n个时间。
求从(0,0)到(n-1,m-1)所需要的最短时间。
如果不存在,照题目格式输出;如果存在,将路径输出。
思路:广搜遍历求出最短路径,并用优先队列优化。
可以用递归逆向遍历出路径(开始没想到)。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
struct Node{
int x,y,step;
friend bool operator<(Node a,Node b)
{
return b.step<a.step;
}
};
char a[][];
int vis[][],fx[][],blood[][];
int n,m,tim,zz[][]={{,},{,-},{,},{-,}};
int bfs()
{
priority_queue <Node> q;
Node tmp;
tmp.x=;tmp.y=;tmp.step=;
vis[][]=-;
q.push(tmp);
while(!q.empty())
{
Node tmp=q.top();
q.pop();
if(tmp.x==n-&&tmp.y==m-) return tmp.step;
for(int i=;i<;i++)
{
Node tp;
tp.x=tmp.x+zz[i][];
tp.y=tmp.y+zz[i][];
if(tp.x<||tp.x>=n||tp.y<||tp.y>=m||vis[tp.x][tp.y]==-) continue;
tp.step=tmp.step++vis[tp.x][tp.y];
vis[tp.x][tp.y]=-;
fx[tp.x][tp.y]=i+;
q.push(tp);
}
}
return -;
}
void Print(int x,int y)
{
int tx,ty;
if(fx[x][y]==) return ;
tx=x-zz[fx[x][y]-][];
ty=y-zz[fx[x][y]-][];
Print(tx,ty);
cout<<tim++<<"s:("<<tx<<","<<ty<<")->("<<x<<","<<y<<")"<<endl;
while(blood[x][y]--) cout<<tim++<<"s:FIGHT AT ("<<x<<","<<y<<")"<<endl;
}
int main(void)
{
int i,j;
while(cin>>n>>m)
{
memset(fx,,sizeof(fx));
memset(blood,,sizeof(blood));
for(i=;i<n;i++) scanf("%s",a[i]);
for(i=;i<n;i++)
for(j=;j<m;j++)
{
if(a[i][j]=='.') vis[i][j]=;
else if(a[i][j]=='X') vis[i][j]=-;
else vis[i][j]=a[i][j]-'',blood[i][j]=vis[i][j];
}
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;
tim=;
Print(n-,m-);
}
cout<<"FINISH"<<endl;
}
return ;
}
hdu-1026(bfs+优先队列)的更多相关文章
- HDU 1026 (BFS搜索+优先队列+记录方案)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 题目大意:最短时间内出迷宫.迷宫里要杀怪,每个怪有一定HP,也就是说要耗一定时.输出方案. 解 ...
- hdu 1026(BFS+输出路径) 我要和怪兽决斗
http://acm.hdu.edu.cn/showproblem.php?pid=1026 模拟一个人走迷宫,起点在(0,0)位置,遇到怪兽要和他决斗,决斗时间为那个格子的数字,就是走一个格子花费时 ...
- HDU 2822 (BFS+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2822 题目大意:X消耗0,.消耗1, 求起点到终点最短消耗 解题思路: 每层BFS的结点,优先级不同 ...
- hdu 1242(BFS+优先队列)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- hdu 1026 bfs+记录路径
题意:从0,0点出发到n-1,m-1点,路上的数字代表要在这个点额外待多少秒,求最短的路 递归输出路径即可 #include<cstdio> #include<iostream> ...
- HDU 1026 BSF+优先队列+记录路径、
#include<iostream> #include<cmath> #include<cstring> #include<cstdio> #inclu ...
- 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 1428 漫步校园 (BFS+优先队列+记忆化搜索)
题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...
- HDU 1242 -Rescue (双向BFS)&&( BFS+优先队列)
题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...
- hdu 2102 A计划 具体题解 (BFS+优先队列)
题目链接:pid=2102">http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 開始看到四分之中的一个的AC率感 ...
随机推荐
- 在Apache下开启SSI配置
开启SSI:html.shtml页面include网页文件 使用SSI(Server Side Include)的html文件扩展名,SSI(Server Side Include),通常称为&quo ...
- vue深入了解组件——Prop
一.Prop的大小写(camelCase vs kebab-case) HTML中的特性名是大小写不敏感的,所以浏览器会把所有大写字符解释为小写字符.这意味着当你使用DOM中的模板时,cameCase ...
- China cuts bank reserves by $100m to cushion US tariffs
China cuts bank reserves by $100m to cushion US tariffs中国央行定向降准释放千亿美元资金China is cutting the amount o ...
- 迷你MVVM框架 avalonjs 学习教程16、过滤器
avalon的过滤器是参考自angular与rivets.它也被称做管道文本过滤器,它的处理对象只能是文本(字符串),它只能用在文本绑定中,并且只能是双花括号形式.下面是各大家的过滤器比较: rive ...
- adb连接过程中常见问题解决方法
在测试过程中经常会遇到需要使用adb连接服务器的问题,但是有时候经常会遇到连不上的情况,总结两种解决方式 1)error: unknown host service 此问题是由于端口号已经被占用了,可 ...
- 吴裕雄 实战python编程(2)
from urllib.parse import urlparse url = 'http://www.pm25x.com/city/beijing.htm'o = urlparse(url)prin ...
- 一些unity问题的收集
---恢复内容开始--- 1.Mono打不开且鼠标点击标签页无反应的解决办法 http://answers.unity3d.com/questions/574157/monodevelop-not-o ...
- 使用生活实例理解Asp.net运行时
学习编程语言,掌握面向对象的编程思想尤为重要,一旦理解了面向对象的这种概念,那么好些地方拿到生活中去理解,就容易的多了.书本上的枯燥干涩的语言,对于好多人来说,即难懂,更难长时间牢牢记得.但是编程语言 ...
- 转)Ubuntu14安装wireshark进行抓包
转自:http://jingyan.baidu.com/article/c74d60009d992f0f6a595de6.html 背景: ubuntu14.04/64位 为了抓包和分析包 安装过程: ...
- swift - 封装 GCDTimer 和 NSTimer
封装的类代码 import UIKit /// 控制定时器的类 class ZDTimerTool: NSObject { /// 定时器 // private var timer: Timer? / ...