http://acm.hdu.edu.cn/showproblem.php?pid=1026

记录bfs路径,用一个数组记录next前驱的方向,然后递归的时候减回去即可

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue> using namespace std ;
const int INF=0xfffffff; int n,m;
char M[][]; struct node{
int x,y,step;
friend bool operator <(node a,node b){
return a.step>b.step;
}
}; int dx[]={,-,,};
int dy[]={,,,-};
int vis[][]; int rd[][]; int bfs(){
memset(vis,,sizeof(vis));
priority_queue <node> q;
node s;
s.x=;s.y=;s.step=;
if(M[][]!='X' && M[n-][m-]!='X')q.push(s);
while(!q.empty()){
node u=q.top();
q.pop();
if(u.x==n- && u.y==m-)return u.step;
for(int i=;i<;i++){
int xx=u.x+dx[i];
int yy=u.y+dy[i];
if(xx< || xx>=n || yy< || yy>=m)continue;
if(M[xx][yy]=='X')continue;
if(vis[xx][yy])continue;
vis[xx][yy]=;
node next=u;
next.x=xx;next.y=yy;next.step++;
if(M[xx][yy]>='' && M[xx][yy]<='')
next.step+=(M[xx][yy]-'');
rd[xx][yy]=i;
q.push(next);
}
}
return INF;
}
int tt;
void output(int x,int y){
if(rd[x][y]==-)return;
int xx=x-dx[rd[x][y]];
int yy=y-dy[rd[x][y]];
output(xx,yy);
printf("%ds:(%d,%d)->(%d,%d)\n",tt++,xx,yy,x,y);
if(M[x][y]>='' && M[x][y]<=''){
int nn=M[x][y]-'';
while(nn--){
printf("%ds:FIGHT AT (%d,%d)\n",tt++,x,y);
}
}
} int main(){
while(~scanf("%d%d",&n,&m)){
for(int i=;i<n;i++)
scanf("%s",M[i]);
int ans=bfs();
if(ans==INF)puts("God please help our poor hero.");
else{
printf("It takes %d seconds to reach the target position, let me show you the way.\n",ans);
rd[][]=-;
tt=;
output(n-,m-);
}
puts("FINISH");
}
return ;
}

HDU 1026的更多相关文章

  1. HDU 1026 Ignatius and the Princess I(带路径的BFS)

    http://acm.hdu.edu.cn/showproblem.php?pid=1026 题意:给出一个迷宫,求出到终点的最短时间路径. 这道题目在迷宫上有怪物,不同HP的怪物会损耗不同的时间,这 ...

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

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

  3. hdu 1026 Ignatius and the Princess I (bfs+记录路径)(priority_queue)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1026 Problem Description The Princess has been abducted ...

  4. hdu 1026 Ignatius and the Princess I

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1026 Ignatius and the Princess I Description The Prin ...

  5. hdu 1026 Ignatius and the Princess I【优先队列+BFS】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 http://acm.hust.edu.cn/vjudge/contest/view.action ...

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

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

  7. hdu 1026 Ignatius and the Princess I(BFS+优先队列)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1026 Ignatius and the Princess I Time Limit: 2000/100 ...

  8. hdu 1026(Ignatius and the Princess I)BFS

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  9. HDU 1026 Ignatius and the Princess I (BFS)

    题目链接 题意 : 从(0,0)点走到(N-1,M-1)点,问最少时间. 思路 : BFS..... #include <stdio.h> #include <string.h> ...

随机推荐

  1. Visual Studio 2010 类模板的修改

    第一步:找到类文件模板路径 C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\C ...

  2. 解决qt5在ubuntu下无法调用fcitx输入中文的问题

    如题,在以前安装qt5.2.1的时候就遇到了这个问题.当时上网搜了很多资料,结果都解决不了,发现都是复制来复制去. 这次因为要用qt5.3.0在ubuntu下写个程序,所以不解决这个问题不行了.就继续 ...

  3. 5月23日 JavaScript练习:累加求和

    第一种方法: 第二种方法:

  4. 详解Jquery和AngularJs,Servlet中jsonp解决跨域问题(转)

    众所周知,jsonp可以解决跨域问题,下面是我在查阅资料和实际项目使用后的一些总结. Jquery中jsonp的使用 //myUrl = "http://localhost:8090/api ...

  5. php变量与数组相互转换的方法(extract与compact

    #php变量与数组相互转换的方法(extract与compact) #compact 多个变量转数组 $name = 'sui'; $email = 'sui@qq.com'; $arr = comp ...

  6. 在 Visual C# 项目中调用 VBA 中的代码

    https://msdn.microsoft.com/zh-cn/library/Bb608613.aspx http://www.cnblogs.com/yangbin1005/archive/20 ...

  7. 转: CSS中float和clear的理解

    float:浮动,比如,默认的,我们知道,div是占满一行的,现在我们想把两个div显示在一行上,那怎么办呢<div style="width:100px;">1111 ...

  8. ncs安装及初次运行

    Tail-f NCS 作为网络配置程序和基础设备之间的接口,能够展现各种服务,修改各开发商不相同的设备配置,同时能及时同步网络设备状态到cdb(configuration database,配置数据库 ...

  9. HDU 4035 Maze 概率dp,树形dp 难度:2

    http://acm.hdu.edu.cn/showproblem.php?pid=4035 求步数期望,设E[i]为在编号为i的节点时还需要走的步数,father为dfs树中该节点的父节点,son为 ...

  10. ECMAScript 6新特性(1)数组篇

    数组现有的方法: .concat():连接两个或更多的数组,并返回结果. .join():把数组的所有元素放入一个字符串.元素通过指定的分隔符进行分隔. .pop():删除并返回数组的最后一个元素 . ...