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. wordpress+php+mysql 配置

    下载并解压wordpress之后,在mysql新建一个数据库,命名,例如testDB1,然后在IIS中新建虚拟目录,指向wordress所在的目录,删除wordpress目录下的wp-config.p ...

  2. matlab可变参数

    Varargin Nargin if nargin == 2 a1 = varargin{1}; a2 = varargin{2};

  3. Oracle select case when

    Case具有两种格式.简单Case函数和Case搜索函数. --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END ...

  4. Halcon 10.0:Hobject图像转CBitmap

    void HImage2CBitmap(Hobject pImage,CBitmap *wImage) { char lpcsType[MAX_STRING]; Hlong lPointer,widt ...

  5. linux下安装vtune_amplifier_xe_2015_update4

    说明:        linux系统: CentOS 6.0 Vtune版本: 2015 安装过程: 1.下载vtune_amplifier_xe_2015_update4.tar.gz(到官网去下载 ...

  6. RM报表 刷新打印机列表

    procedure TRMReport.ShowPreparedReport; var s: string; lPreviewForm: TRMPreviewForm; begin RMCurRepo ...

  7. WP8.1 Study12:文件压缩与Known Folder(包含SD卡操作)

    一.文件压缩 当应用程序保存和加载数据,它可以使用压缩. 1.使用 Windows.Storage.Compression.Compressor 压缩,获得一个Compressor stream. v ...

  8. Rhel6-lvs配置文档

    系统环境: rhel6 x86_64 iptables and selinux disabled 相关网址:http://zh.linuxvirtualserver.org/ yum仓库配置: [rh ...

  9. 展辰涂料如何利用K2BPM加强流程管控?

    展辰涂料集团股份有限公司是一家集环保涂料研发.生产.销售和工程施工为一体的国家级高新技术企业.展辰涂料集团股份有限公司是我国最大的民族涂料企业之一,自成立以来就高度注重产品技术研发,截至目前,公司已取 ...

  10. scrollView自动加载数据demo

    package combaidu.mylistsrollview; import java.util.ArrayList;import java.util.List; import com.baidu ...