题目链接: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. Gson 解析教程

    Gson 是google解析Json的一个开源框架,同类的框架fastJson,JackJson等等 本人fastJson用了两年,也是从去年才开始接触Gson,希望下面的总结会对博友有用,至于Gso ...

  2. linux c++下载http文件并显示进度<转>

    #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <net ...

  3. centos7 jenkins 安装

    前提: 安装了 jdk ,我的是jdk8 第一步: https://jenkins.io/download/ 下载 可以下载 rpm文件, 标红处, 也可以下载war包(Generic Java pa ...

  4. caffe openpose/Realtime Multi-Person 2D Pose Estimation using Part Affinity Fields配置(转)

    Realtime Multi-Person 2D Pose Estimation using Part Affinity Fields 是CVPR2017的一篇论文,作者称是世界上第一个基于深度学习的 ...

  5. Ubuntu 安装 Elasticsearch

    1.安装java 注意:最新版本的elasticsearch(5.6.2)要求安装java8 1.sudo apt-add-repository ppa:webupd8team/java 2.sudo ...

  6. 求数组中的逆序对的数量----剑指offer36题

    在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数: 如数组{7,5,6,4},逆序对总共有5对,{7,5},{7,6},{7, ...

  7. Mysql两个time类型计算时间相减

    round((UNIX_TIMESTAMP(finishtime)-UNIX_TIMESTAMP(starttime))/60) 得到的时间是分钟数

  8. 全国省市区数据库SQL(有可能不是最新的)

    百度云下载地址:https://pan.baidu.com/s/1lStN7tYpwOtpC-r3G2X2sw

  9. 常用修图工具的一些使用技巧及问题解决方法——ai

    一.ai如何修改画布大小 一. ai如何修改画布大小: 1. 左上角菜单中的文件——文档设置(也可以直接点菜单栏下边的控制栏中的文档设置) 2. 文档设置界面中,点击右上角“编辑画板“ 3. 此时面板 ...

  10. 【趣】Python获取变量的变量名

    两种不完美的方式: 用locals,globals 用locals获取变量列表,再遍历比较对象. def namestr(obj): ns = globals() return [name for n ...