题意:从0,0点出发到n-1,m-1点,路上的数字代表要在这个点额外待多少秒,求最短的路

递归输出路径即可

 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
using namespace std;
#define MOD 1000000007
const int INF=0x3f3f3f3f;
const double eps=1e-;
#define cl(a) memset(a,0,sizeof(a))
#define ts printf("*****\n");
const int MAXN=;
int n,m,tt;
int sumt=;
struct node
{
int x,y,t;
node(){}
node(int xx,int yy,int tt)
{
x=xx,y=yy,t=tt;
}
friend bool operator<(node a,node b)
{
return a.t>b.t;
}
};
bool vis[MAXN][MAXN];
int dir[MAXN][MAXN];
char maze[MAXN][MAXN];
int d[][]={,,,,-,,,-};
int kk=;
int outpath(int x,int y) //递归输出,从x,y出发所在的时间
{
if(x==&&y==)
{
int nt=;
printf("%ds:(%d,%d)->",nt,x,y);
return nt;
}
else
kk=dir[x][y];
int nt=outpath(x-d[kk][],y-d[kk][]);
printf("(%d,%d)\n",x,y);
if(<maze[x][y]-''&&maze[x][y]-''<=)
{
int l=maze[x][y]-'';
while(l--)
printf("%ds:FIGHT AT (%d,%d)\n",++nt,x,y);
}
if(nt==sumt) return ;
printf("%ds:(%d,%d)->",++nt,x,y);
return nt;
}
void bfs()
{
node now,next;
priority_queue<node> q;
q.push(node(,,));
vis[][]=;
while(!q.empty())
{
now=q.top();
q.pop();
if(now.x==n-&&now.y==m-)
{
printf("It takes %d seconds to reach the target position, let me show you the way.\n",now.t);
sumt=now.t;
outpath(n-,m-);
printf("FINISH\n");
return;
}
for(int i=;i<;i++)
{
next.x=now.x+d[i][];
next.y=now.y+d[i][];
next.t=now.t+;
if(next.x>=&&next.y>=&&next.x<n&&next.y<m&&!vis[next.x][next.y]&&maze[next.x][next.y]!='X')
{
if(''<maze[next.x][next.y]&&maze[next.x][next.y]<='')
{
next.t+=maze[next.x][next.y]-'';
vis[next.x][next.y]=;
dir[next.x][next.y]=i;
q.push(next);
}
else
{
dir[next.x][next.y]=i;
vis[next.x][next.y]=;
q.push(next);
}
}
}
}
puts("God please help our poor hero.\nFINISH");
}
int main()
{
int i,j,k;
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
#endif
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=;i<n;i++)
{
scanf("%s",maze[i]);
}
cl(vis);
bfs();
}
}

hdu 1026 bfs+记录路径的更多相关文章

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

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

  2. HDU1026--Ignatius and the Princess I(BFS记录路径)

    Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has ...

  3. POJ.3894 迷宫问题 (BFS+记录路径)

    POJ.3894 迷宫问题 (BFS+记录路径) 题意分析 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, ...

  4. Codeforces-A. Shortest path of the king(简单bfs记录路径)

    A. Shortest path of the king time limit per test 1 second memory limit per test 64 megabytes input s ...

  5. 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 ...

  6. hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)

    以前写的题了,现在想整理一下,就挂出来了. 题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1). 'X'为墙,'.'为路,数字为怪物.墙不能走,路花1s经过,怪物需要 ...

  7. hdu 1226 BFS + bfs记录路径

    http://acm.hdu.edu.cn/showproblem.php? pid=1226 为了节省空间.您可以使用vis初始化数组初始化-1. 发现BFSeasy错了地方 始一直WA在这里:就是 ...

  8. (简单) POJ 3414 Pots,BFS+记录路径。

    Description You are given two pots, having the volume of A and B liters respectively. The following ...

  9. 迷宫问题(bfs+记录路径)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105278#problem/K K - 迷宫问题 Time Limit:1000 ...

随机推荐

  1. weblogica 启动managed server 不用每次输入密码

    [weblogic@node2 AdminServer]$ pwd /home/weblogic/Oracle/Middleware/Oracle_Home/user_projects/domains ...

  2. 記一次undo問題

    記一次undo問題 參考:http://www.linuxidc.com/Linux/2014-06/103780.htm ORA-00376: 無法於此時讀取檔案 3 ORA-01110: 資料檔 ...

  3. Linux USB驱动学习总结(一)---- USB基本概念及驱动架构

    USB,Universal Serial Bus(通用串行总线),是一个外部总线标准,用于规范电脑与外部设备的连接和通讯.是应用在PC领域的接口技术.USB接口支持设备的即插即用和热插拔功能.USB是 ...

  4. Oracle 11G R2 RAC中的scan ip 的用途和基本原理【转】

    Oracle 11G R2 RAC增加了scan ip功能,在11.2之前,client链接数据库的时候要用vip,假如你的cluster有4个节点,那么客户端的tnsnames.ora中就对应有四个 ...

  5. 【转】GridView 加载空行并点击编辑每一个单元格

    代码 <script runat="server"> protectedvoid Button1_Click(object sender, System.EventAr ...

  6. 【hihocoder1251】Today is a rainy day

    #include<bits/stdc++.h> ; ; const int inf=0x3f3f3f3f; using namespace std; char s1[N],s2[N]; ] ...

  7. PHP计算字符串的长度

    <?php /** * 计算字符串的长度(汉字按照两个字符计算) * * @param string $str 字符串 * * @return int */ function str_len($ ...

  8. Luogu P3384 【【模板】树链剖分】

    转载请注明出处,部分内容引自banananana大神的博客 ~~别说你不知道什么是树~~╮(─▽─)╭(帮你百度一下) 先来回顾两个问题:1,将树从x到y结点最短路径上所有节点的值都加上z 这也是个模 ...

  9. SQL语句资料

    --语 句 功 能  --数据操作  SELECT --从数据库表中检索数据行和列  INSERT --向数据库表添加新数据行  DELETE --从数据库表中删除数据行  UPDATE --更新数据 ...

  10. 2015309南皓芯实验二 Java面向对象程序设计

    一.实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 实验要求 1.没有Linux基础的同学建议先学习< ...