hdu 1026 bfs+记录路径
题意:从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+记录路径的更多相关文章
- hdu 1026(BFS+输出路径) 我要和怪兽决斗
http://acm.hdu.edu.cn/showproblem.php?pid=1026 模拟一个人走迷宫,起点在(0,0)位置,遇到怪兽要和他决斗,决斗时间为那个格子的数字,就是走一个格子花费时 ...
- HDU1026--Ignatius and the Princess I(BFS记录路径)
Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has ...
- POJ.3894 迷宫问题 (BFS+记录路径)
POJ.3894 迷宫问题 (BFS+记录路径) 题意分析 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, ...
- 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 ...
- 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 ...
- hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)
以前写的题了,现在想整理一下,就挂出来了. 题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1). 'X'为墙,'.'为路,数字为怪物.墙不能走,路花1s经过,怪物需要 ...
- hdu 1226 BFS + bfs记录路径
http://acm.hdu.edu.cn/showproblem.php? pid=1226 为了节省空间.您可以使用vis初始化数组初始化-1. 发现BFSeasy错了地方 始一直WA在这里:就是 ...
- (简单) POJ 3414 Pots,BFS+记录路径。
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- 迷宫问题(bfs+记录路径)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105278#problem/K K - 迷宫问题 Time Limit:1000 ...
随机推荐
- Python——脚本(calculator)
<Python基础教程>(第二版) P123 书中原代码如下: class Calculator: def calculator(self,expression): self.value ...
- 利用正则表达式去除所有html标签,只保留文字
后台将富文本编辑器中的内容返回到前端时如果带上了标签,这时就可以利用这种方法只保留文字. 标签的格式有以下几种 1.<div class="test"></div ...
- C++ 和 MFC的学习
1. 在Windows应用程序设计中,既可以使用个C的基本数据类型,也可以使用Windows自定义的数据类型.但要注意,凡是Windows自定义的关键字都要大写. 2. 什么是句柄? 在Windows ...
- 【HASPDOG】hasp_update参数f和i区别
[root@BICServer-TX shared]# ./hasp_update This is a simple demo program for the Sentinel Update and ...
- csslint在前端项目中的使用
大家都听说过jslint,eslint,不过你可能没见过csslint,你可能会问csslint有什么用,为什么今天要说csslint,是因为我在开发中遇到一个坑,其实之前不怎么使用csslint的, ...
- PHP实现菜单无限极分类
菜单数据 这里我们的菜单数据是临时数据, 没有从数据库中查询处理,数据基本和数据库中的的相似. 数据如下: $items = array( 1 => array('id' => 1, 'p ...
- SP_attach_db 添加数据库文件
SP_attach_db 用法如下: EXEC SP_attach_db @dbname = N'目标数据库名', //这是你要引入后的数据库名. ...
- 20155225 2016-2017-2《Java程序设计》课程总结
20155225 2016-2017-2<Java程序设计>课程总结 每周作业链接汇总 预备作业1:新的开始 预备作业2:C语言学习回顾 预备作业3:Linux基础入门和虚拟机的安装 第一 ...
- CF 586A 找1的个数和101的个数
Sample test(s) input 50 1 0 1 1 output 4 input 71 0 1 0 0 1 0 output 4 input 10 output 0 # include & ...
- codis+redis集群学习整理(待续)
Codis 由四部分组成: Codis Proxy (codis-proxy) Codis Manager (codis-config) Codis Redis (codis-server) ZooK ...