hdu 1026(BFS+输出路径) 我要和怪兽决斗
http://acm.hdu.edu.cn/showproblem.php?pid=1026
模拟一个人走迷宫,起点在(0,0)位置,遇到怪兽要和他决斗,决斗时间为那个格子的数字,就是走一个格子花费时间1,
遇到有数字的格子还要花费这个数字大小的时间,输出最后走到(n-1,m-1)的最小时间,还要输出他的路径,'X'是墙,‘.’是可以走的空地
就是这个路径,刚一看题看到那个样例输出吓得我都飞起来了,好多啊!
其实还好啦,广搜,一开始还因为是普通的广搜,后来发现不一样,
为了寻求最小时间,他可以选择走有数字的格子也可以选择绕过去,看哪个时间短一些
首先求出最小时间,跟那个连连看差不多,用visit数组初始化很大,来储存到达每个点的最小时间
然后输出路径的时候用递归回溯,因为广搜是广泛的搜索,并不能保证走过的点都是最短路径上的点
所以从终点开始利用他的方向来往前回溯,注意因为没有用优先队列和输出漏了句号所以刚开始wa了几发
code
#include<cstdio>
#include<climits>
#include<queue>
#include<cstring>
using namespace std;
char map[][];
int visit[][];
int flag[][];
struct point{
int x,y;
int time;
friend bool operator<(point n1,point n2)
{
return n2.time<n1.time;
}
};
int n,m,t;
int dx[]={,,,-};
int dy[]={,-,,};
int bfs()
{
int i;
priority_queue<point>Q;
point now,next;
now.x=;now.y=;
now.time=;
visit[][]=;
Q.push(now);
while (!Q.empty())
{
now=Q.top();
Q.pop();
if (now.x==n-&&now.y==m-)
return visit[now.x][now.y];
for (i=;i<;i++)
{
next.x=now.x+dx[i];
next.y=now.y+dy[i];
if (next.x<||next.x>=n||next.y<||next.y>=m)continue;
if (map[next.x][next.y]=='X')continue;
if (map[next.x][next.y]=='.')
next.time=now.time+;
else
next.time=now.time++(map[next.x][next.y]-'');
if (map[next.x][next.y]<=''&&map[next.x][next.y]>=''&&visit[next.x][next.y]>now.time+(map[next.x][next.y]-''))
{
visit[next.x][next.y]=now.time++(map[next.x][next.y]-'');
flag[next.x][next.y]=i+;
Q.push(next);
}
else if (map[next.x][next.y]=='.'&&visit[next.x][next.y]>now.time+)
{
visit[next.x][next.y]=now.time+;
flag[next.x][next.y]=i+;
Q.push(next);
}
}
}
return -;
}
void output(int x,int y)//回溯
{
int sx,sy;
if (flag[x][y]==) return ;
sx=x-dx[flag[x][y]-];
sy=y-dy[flag[x][y]-];
output(sx,sy);
printf("%ds:(%d,%d)->(%d,%d)\n",t++,sx,sy,x,y);
if (map[x][y]<=''&&map[x][y]>='')
{
int w=map[x][y]-'';
while (w--)
printf("%ds:FIGHT AT (%d,%d)\n",t++,x,y);
}
}
int main()
{
int i,j,q;
while (~scanf("%d %d",&n,&m))
{
getchar();
for (i=;i<n;i++)
{
for (j=;j<m;j++)
scanf(" %c",&map[i][j]);
}
for (i=;i<n;i++)
for (j=;j<m;j++)
{
visit[i][j]=INT_MAX;
flag[i][j]=;
}
q=bfs();
if (q==-)
{
puts("God please help our poor hero.");
puts("FINISH");
}
else
{
printf("It takes %d seconds to reach the target position, let me show you the way.\n",q);
t=;
output(n-,m-);
puts("FINISH");
}
}
return ;
}
hdu 1026(BFS+输出路径) 我要和怪兽决斗的更多相关文章
- hdu 1026 bfs+记录路径
题意:从0,0点出发到n-1,m-1点,路上的数字代表要在这个点额外待多少秒,求最短的路 递归输出路径即可 #include<cstdio> #include<iostream> ...
- hdu 1385 Floyd 输出路径
Floyd 输出路径 Sample Input50 3 22 -1 43 0 5 -1 -122 5 0 9 20-1 -1 9 0 44 -1 20 4 05 17 8 3 1 //收费1 3 // ...
- HDU 1026 (BFS搜索+优先队列+记录方案)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 题目大意:最短时间内出迷宫.迷宫里要杀怪,每个怪有一定HP,也就是说要耗一定时.输出方案. 解 ...
- hdu 1503 LCS输出路径【dp】
hdu 1503 不知道最后怎么输出,因为公共部分只输出一次.有人说回溯输出,感觉好巧妙!其实就是下图,输出的就是那条灰色的路径,但是初始时边界一定要初始化一下,因为最第一列只能向上走,第一行只能向左 ...
- 蓝桥T291(BFS + 输出路径)
http://lx.lanqiao.org/problem.page?gpid=T291 学霸的迷宫 时间限制:1.0s 内存限制:256.0MB 问题描述 学霸抢走了大家的作业,班 ...
- poj 3414 Pots(bfs+输出路径)
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- bfs输出路径 && 最短路(迪杰斯特拉)输出路径
问题描述 解决方法 1.像第一个问题那就是最短路问题(我代码采用迪杰斯特拉算法)实现 2.换乘次数最少,那就用bfs广搜来寻找答案.但是我的代码不能保证这个最少换乘是最短路程 代码 1 #includ ...
- 地下迷宫(bfs输出路径)
题解:开一个pre数组用编号代替当前位置,编号用结构题另存,其实也可以i*m+j来代替,我写的有点麻烦了; 代码: #include <iostream> #include <cst ...
- HDU 1026 Ignatius and the Princess I(带路径的BFS)
http://acm.hdu.edu.cn/showproblem.php?pid=1026 题意:给出一个迷宫,求出到终点的最短时间路径. 这道题目在迷宫上有怪物,不同HP的怪物会损耗不同的时间,这 ...
随机推荐
- asp.net4.0
asp.net4.0安装路径:C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -i
- spring boot 程序打jar包及运行
- Ik分词器没有使用---------elasticsearch-analysis-ik 5.6.3分词问题
此文章在作者认真阅读源码后发现,这并不是问题所在. 此篇文章是对IK配置的错误理解.新版本的IK配置的扩展字典本来就该使用者自己去手动配置! 1.问题 现在项目中用的是ES5.6.3的版本,在解决Fi ...
- intelliJ IDEA 破解,亲测有效
https://blog.csdn.net/shengshengshiwo/article/details/79599761
- godep 包管理
go get -u -v github.com/tools/godep godep save
- JS-cookie和正则表达式
一 cookie 1 什么是cookie? 会话跟踪技术 2 作用 验证身份,存储信息. 3 特点 大小限制,最多存4k: 每个域下只能存50个cookie: 有时间限制: 只能存放字符串: 只能访问 ...
- AJAX简单实例
越用AJAX越觉得它的强大.好用. 平常我们提交表单,是直接通过action属性,直接向后台提交数据. 我们也可以用AJAX向后台提交数据.例如: 这是一个表单,两个字段:notice,scort,保 ...
- ATM--代码
//信1705-2 张小军 20173662 import java.io.*; import java.util.ArrayList; import java.util.Scanner;public ...
- cdh 安装系列3--cdh manager 安装 cdh 6.01 版本
安装前提是cdh manager 已经可以通过admin登录,管理台安装在192.168.20.163 一.安装自动TLS Setup Auto-TLS 1.ssh 192.168.20.163 2. ...
- Windows配置Java环境
https://jingyan.baidu.com/article/84b4f56598d88b60f7da3272.html