题目链接: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+优先队列)的更多相关文章

  1. HDU 1026 (BFS搜索+优先队列+记录方案)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 题目大意:最短时间内出迷宫.迷宫里要杀怪,每个怪有一定HP,也就是说要耗一定时.输出方案. 解 ...

  2. hdu 1026(BFS+输出路径) 我要和怪兽决斗

    http://acm.hdu.edu.cn/showproblem.php?pid=1026 模拟一个人走迷宫,起点在(0,0)位置,遇到怪兽要和他决斗,决斗时间为那个格子的数字,就是走一个格子花费时 ...

  3. HDU 2822 (BFS+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2822 题目大意:X消耗0,.消耗1, 求起点到终点最短消耗 解题思路: 每层BFS的结点,优先级不同 ...

  4. hdu 1242(BFS+优先队列)

    Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  5. hdu 1026 bfs+记录路径

    题意:从0,0点出发到n-1,m-1点,路上的数字代表要在这个点额外待多少秒,求最短的路 递归输出路径即可 #include<cstdio> #include<iostream> ...

  6. HDU 1026 BSF+优先队列+记录路径、

    #include<iostream> #include<cmath> #include<cstring> #include<cstdio> #inclu ...

  7. 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 ...

  8. HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)

    题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...

  9. HDU 1242 -Rescue (双向BFS)&amp;&amp;( BFS+优先队列)

    题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...

  10. hdu 2102 A计划 具体题解 (BFS+优先队列)

    题目链接:pid=2102">http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 開始看到四分之中的一个的AC率感 ...

随机推荐

  1. DataSanp App与Rest, WebBroker App的区别

    DataSanp App与Rest, WebBroker App的区别 datasnap server :选择这一项,我们得到的将是一个独立EXE的三层服务器应用程序(TCP及HTTP两种模式) To ...

  2. 突破MSDE 的2GB数据限制

    Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\MSSQLServer\ ...

  3. 机器学习入门-贝叶斯中文新闻分类任务 1. .map(做标签数字替换) 2.CountVectorizer(词频向量映射) 3.TfidfVectorizer(TFDIF向量映射) 4.MultinomialNB()贝叶斯模型构建

    1.map做一个标签的数字替换 2.vec = CountVectorizer(lowercase=False, max_features=4000)  # 从sklean.extract_featu ...

  4. 在eclipse的web项目里面创建jsp时第一行报错

    原因是因为项目里面没有配置tomcat,配置一下tomcat就好了

  5. python传参数

    Python参数传递采用的肯定是“传对象引用”的方式.这种方式相当于传值和传引用的一种综合.如果函数收到的是一个可变对象(比如字典或者列表)的引用,就能修改对象的原始值--相当于通过“传引用”来传递对 ...

  6. No matter how hard it is or no matter how bad it gets, I am going to make it!

    No matter how hard it is or no matter how bad it gets, I am going to make it! He always had a yearni ...

  7. Kotlin语言学习笔记(7)

    反射 // 反射 val c = MyClass::class val c2 = MyClass::class.java // 获取KClass的引用 val widget: Widget = ... ...

  8. java编写一个汽车类,有属性:品牌、型号、排量、速度,有方法:启动、加速、转弯、刹车、息火

    /* * 汽车实体类 * 类里面有属性和方法 */public class Car {    String  brand;   //汽车品牌    String modelNumber; //汽车型号 ...

  9. mysql 中文编码问题

  10. Week4-作业1:阅读笔记与思考

    我在这三天时间里阅读了<构建之法>的第四章和第十七章,产生了一些疑问和深层次的思考. 第四章 问题1: 书中第68页提到“注释(包括所有源代码)应该只用ASCII字符,不要用中文或其他特殊 ...