题目链接: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. c++ static成员

    static 成员通常不能在类的定义体重初始化 有一种例外,const static成员可以在定义体内初始化,并且可以用于构造函数 将函数声明为const表示该函数不能修改其所属的对象

  2. 原生js实现三级复选框

    工作中要做一个三级的复选框,用js实现了一下,从项目中把相关代码抽取出来了,有相关需求的可以参考一下.亲测可用. <!DOCTYPE html> <html> <head ...

  3. 一个简单例子弄懂什么是javascript函数劫持

    javascript函数劫持很简单,一般情况下,只要在目标函数触发之前,重写这个函数即可. 比如,劫持eval函数的代码如下: var _eval=eval; eval=function(x){ if ...

  4. Spring Boot 菜鸟入门(持续更新)

    目录 问题一 Note 最近入了Java的坑,正在学习spring boot.记录一下遇到的问题吧. 问题一 请求参数的问题 /get/bob我想获取bob @RequestMapping(value ...

  5. Struts2拦截器的配置

    struts2拦截器interceptor的三种配置方法方法1. 普通配置法 <struts> <package name="struts2" extends=& ...

  6. virtual abstract override new 几点学习

    1.Vitual方法和普通方法区别为:继承其的子类可以用override/new在重载此方法,也可以不重载其方法,有方法体(可以写语句),override修饰则调用子类方法2.abstract类中抽象 ...

  7. jquery 显示 隐藏

    参考 http://www.w3school.com.cn/jquery/jquery_hide_show.asp $("#a").hide(); $("#a" ...

  8. 反射, getClass(), 和something.class以及类型类(转)

    原文地址:http://www.cnblogs.com/lianghui66/archive/2012/12/03/2799134.html 所谓反射,可以理解为在运行时期获取对象类型信息的操作.传统 ...

  9. SVN常见问题I

    TortoiseSVN ->Settings 把权限给清空,不让用户A的权限再保留在里面 右键->SVN Checkout 之后需要在A账户和B账户之间来回切换

  10. asp.net后台解析JSON,并将值赋给对象

    示例代码如下: using System; using System.Collections.Generic; using System.Web.Script.Serialization; publi ...