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. log4j配置文件的详解

    1.配置根Logger,其语法为: log4j.rootLogger = [ level ] , appenderName, appenderName, … 其中,level 是日志记录的优先级,分为 ...

  2. Oracle - PL/SQL Commands

    第一章:日志管理 1.forcing log switches sql> alter system switch logfile; 2.forcing checkpoints sql> a ...

  3. LevelDb简单介绍和原理——本质:类似nedb,插入数据文件不断增长(快照),再通过删除老数据做更新

    转自:http://www.cnblogs.com/haippy/archive/2011/12/04/2276064.html 有时间再好好看下整个文章! 说起LevelDb也许您不清楚,但是如果作 ...

  4. 转载 浅谈C/C++中的static和extern关键字

    浅谈C/C++中的static和extern关键字 2011-04-21 16:57 海子 博客园 字号:T | T   static是C++中常用的修饰符,它被用来控制变量的存贮方式和可见性.ext ...

  5. 并发容器之CopyOnWriteArrayList

    原文链接: http://ifeve.com/java-copy-on-write/ Copy-On-Write简称COW,是一种用于程序设计中的优化策略.其基本思路是,从一开始大家都在共享同一个内容 ...

  6. Linux 中 10 个有用的命令行补全例子

    在 Linux 系统中,当你输入一个命令,再按两次 TAB 键,就会列出所有以你输入字符开头的可用命令.这并不新鲜,可能你已经知道了.这个功能被称作命令行补全bash completion.默认情况下 ...

  7. 部分SIM卡被曝存安全漏洞:7.5亿部手机受牵连

    7月22日消息,据国外媒体报道,一安全研究人员发现部分移动SIM卡所使用的加密方式存在一个安全漏洞,可能会导致手机被黑客远程控制. DES数据加密标准的SIM卡——DES是一种较旧的标准,目前正被部分 ...

  8. 常州培训 day7 解题报告

    最后一天..有些感慨,这七天被虐的感动万分 第一题: 题目大意: 求出 n*i(i=1,2,3....n) mod p的逆元  n<p<=3000000 ,p是质数. 之前写过了,懒得再写 ...

  9. 迭代输出Map和List<Map<String,Object>>的方法

    一.Map<String,Object> String:key的类型 Object:value的类型,value可能是String,或者int类型,什么类型都可以 对于Map接口来说,本身 ...

  10. html5+js实现刮刮卡效果

    通过Canvas实现的可刮涂层效果. 修改img.src时涂层也会自动适应新图片的尺寸. 修改layer函数可更改涂层样式. 涂层: 可刮效果: <!DOCTYPE html> <h ...