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. 最小生成树练习1(克鲁斯卡尔算法Kruskal)

    今天刷一下水题练手入门,明天继续. poj1861 Network(最小生成树)新手入门题. 题意:输出连接方案中最长的单根网线长度(必须使这个值是所有方案中最小的),然后输出方案. 题解:本题没有直 ...

  2. 使用DD_belatedPNG让IE6支持PNG透明图片

    使用DD_belatedPNG让IE6支持PNG透明图片 众所周知IE6不支持透明的PNG图片,而PNG图片在Web设计方面表现力上,具有其它图形格式所达不到的效果,IE6这一致命缺陷极大地限制了We ...

  3. html5 <input> placeholder 属性

    带有 placeholder 文本的搜索字段: <form action="demo_form.asp" method="get"> <inp ...

  4. [转]Web开发的发展史

    之所以转这篇文章是因为它可以对web请求有大致的理解. 以下内容转自:http://www.open-open.com/news/view/19ed96a 英文原文: Full stack web d ...

  5. cookie与sessionID之间的关系实验

    上一篇介绍了cookie,这里来看看cookie与sessionID之间有什么关系. 一.编写测试用例代码 新建一个servlet如下: public class SessionServlet ext ...

  6. / etc / init.d / iptables: line 268: restorecon: command not found

    When I tried to restart iptables from vps , I got the following error. Iptables encountered such a p ...

  7. Shell脚本:使用rsync备份文件/目录

    本文我们介绍一个shell脚本,用来使用rsync命令将你本地Linux机器上的文件/目录备份到远程Linux服务器上.使用该脚本会以交互的方式实施备份,你需要提供远程备份服务器的主机名/ip地址和文 ...

  8. pthread_create如何传递两个参数以上的参数

    涉及多参数传递给线程的,都需要使用结构体将参数封装后,将结构体指针传给线程 定义一个结构体 struct mypara { var para1;//参数1 var para2;//参数2 } 将这个结 ...

  9. Java 类的一些高级特征

    1. 面向对象的特征二:继承性 * 1.为什么要设计继承性? 继承的出现提高了代码的复用性. 继承的出现让类与类之间产生了关系,提供了多态的前提. * 2.通过"class A extend ...

  10. Windows任务计划

    任务计划,可以将任何脚本.程序或文档安排在某个时间运行.“任务计划”在每次启动windows系统的时候自动启动(默认Task Scheduler服务是开启的)并在后台运行.使用“任务计划”可以完成以下 ...