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率感 ...
随机推荐
- JS时间格式和时间戳的互转
//时间格式转为时间戳 function sjc(){ var date = new Date(); //时间对象 var str = date.getTime(); //转换成时间戳 } //时间戳 ...
- ABAP-HTTP发送JSON
1. HTTP发送JSON格式数据: function zap_01_url_sap_to_c3 . *"------------------------------------------ ...
- LVS的DR模式
DR模式: 请求由LVS接受,由真实提供服务的服务器(RealServer, RS)直接返回给用户,返回的时候不经过LVS. DR模式下需要LVS和绑定同一个VIP(RS通过将VIP绑定在loopba ...
- ArcGIS案例学习笔记3_2
ArcGIS案例学习笔记3_2 联系方式:谢老师,135-4855-4328, xiexiaokui#qq.com 时间:第3天下午 内容:CAD数据导入,建库和管理 目的:生成地块多边形,连接属性, ...
- VB.net 与 C# 的对应逻辑运算符
And:对两个Boolean表达式执行逻辑和.AndAlso:与AndAlso类似,关键差异是AndAlso显示短路行为,如果AndAlso中的第一个表达式为False,则不计算第二个表达式.Or:对 ...
- php的websocket
对TCP/IP.UDP.Socket编程这些词你不会很陌生吧?随着网络技术的发展,这些词充斥着我们的耳朵.那么我想问: 1. 什么是TCP/IP.UDP?2. Sock ...
- dubbo dubbo.xsd 报错
构建dubbo项目的时候会遇到: Multiple annotations found at this line: - cvc-complex-type.2.4.c: The matching wil ...
- selenium -- 鼠标悬停
针对页面上的二级菜单,需要鼠标悬停才能进行操作. /** * Clicks (without releasing) in the middle of the given element. This i ...
- Quartz入门
Quartz体系结构: 明白Quartz怎么用,首先要了解Scheduler(调度器).Job(任务)和Trigger(触发器)这3个核心的概念.请注意加粗内容. 1. Job: 是一个接口,只定义一 ...
- Android中WebView使用全解
开始 在Android系统中内嵌的WebKit,这是一个浏览器内核,它帮助着我们可以浏览网页.在实际开发中,如果你想让你的App能够访问网页,那就需要用到WebView这个控件. 如何使用? 其实使用 ...