题意:从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. Framebuffer 驱动学习总结(一) ---- 总体架构及关键结构体

    一.Framebuffer 设备驱动总体架构 帧缓冲设备为标准的字符型设备,在Linux中主设备号29,定义在/include/linux/major.h中的FB_MAJOR,次设备号定义帧缓冲的个数 ...

  2. aarch64_g4

    golang-github-inconshreveable-muxado-devel-0-0.7.gitf693c7e.fc26.noarch.rpm 2017-02-11 16:47 30K fed ...

  3. 使用的vue、elementUI、vuex、express、mongoDB的单页应用

    基于vue.vuex.express.mongodb的一个单页应用,包括前后端,前端主要是使用vue,后端是node的express,数据库是使用的mongodb 1.下载使用 git clone h ...

  4. 说一下怎么搭建外网来访问SVN服务器

    一.搭建SVN服务器 1.所需软件 TortoiseSVN,下载地址http://tortoisesvn.net/downloads.html TortoiseSVN中文语言包,下载地址http:// ...

  5. Shell编程学习1--基础了解

    "#!path"告诉系统path所指的程序为用来解释此脚本文件的Shell程序: 如#!/bin/sh, #!/bin/bash Shell Script的后缀名为.sh; ech ...

  6. TF-搞不懂的TF矩阵加法

    看谷歌的demo mnist,卷积后加偏执量的代码 h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)h_pool1 = max_pool ...

  7. An overview of gradient descent optimization algorithms (更新到Adam)

    Momentum:解快了收敛速度,同时也减弱了SGD的波动 NAG: 减速了Momentum更新参数太快 Adagrad: 出现频率较低参数采用较大的更新,对于出现频率较高的参数采用较小的,不共用一个 ...

  8. java基础37 集合框架工具类Collections和数组操作工具类Arrays

    一.集合框架工具类:Collections 1.1.Collections类的特点 该工具类中所有的方法都是静态的 1.2.Collections类的常用方法 binarySearch(List< ...

  9. javaweb 要学习的东西

    我学院课设是Javaweb程序,要用eclipse,tomcat,jbdc,和数据库 jbdc,是连接数据库的驱动,tomcat是一种类似于服务器的东西,现在买不起服务器,就用tomcat

  10. 解决Url带中文参数乱码问题

    这里我来介绍下如何配置Tomcat 来解决Url带中文参数乱码问题: 首先打开Tomcat安装目录,以Tomcat7为例,其他版本基本一样: 打开conf文件 打开server.xml 大概在70行左 ...