题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1026

题目大意:最短时间内出迷宫。迷宫里要杀怪,每个怪有一定HP,也就是说要耗一定时。输出方案。

解题思路

要是没有输出方案,就是一个简单粗暴的BFS。

一开始解决输出方案问题时,简单粗暴地在每次状态里加个vector,然后连带vector一起转移。

结果vector的push_back实在太慢,无论怎么优化都是T。

于是参考了ACMan同学的方案,path[X][Y]记录下父亲点。

最后输出的时候,要么递归输出,要么就选择从终点BFS, 这样就可以顺序输出。

#include "cstdio"
#include "cstring"
#include "string"
#include "iostream"
#include "queue"
#include "vector"
using namespace std;
#define inf 1<<28
struct status
{
int x,y,dep;
status() {}
status(int x,int y,int dep):x(x),y(y),dep(dep) {}
bool operator < (const status &a) const
{
return dep > a.dep;
}
};
int n,m,dir[][]={-,,,,,-,,},map[][],spec[][],vis[][],ans;
status path[][];
void bfs(int x,int y)
{
priority_queue<status> Q;
Q.push(status(x,y,map[x][y]-));
vis[x][y]=true;
bool flag=false;
while(!Q.empty())
{
if(flag) break;
status t=Q.top();Q.pop();
for(int s=;s<;s++)
{
int X=t.x+dir[s][],Y=t.y+dir[s][];
if(vis[X][Y]||X<||X>=n||Y<||Y>=m||!map[X][Y]) continue;
vis[X][Y]=true;
path[X][Y].x=t.x;
path[X][Y].y=t.y;
Q.push(status(X,Y,t.dep+map[X][Y]));
if(X==&&Y==) {flag=true;ans=min(ans,t.dep+map[X][Y]);break;}
}
}
}
int main()
{
ios::sync_with_stdio(false);
string tt;
while(cin>>n>>m)
{
memset(vis,,sizeof(vis));
memset(spec,,sizeof(spec));
ans=inf;
for(int i=; i<n; i++)
{
cin>>tt;
for(int j=; j<tt.size(); j++)
{
if(tt[j]=='X') map[i][j]=;
else if(tt[j]=='.') map[i][j]=;
else {map[i][j]=tt[j]-''+;spec[i][j]=tt[j]-'';}
}
}
bfs(n-,m-);
if(ans==inf) printf("God please help our poor hero.\n");
else
{
printf("It takes %d seconds to reach the target position, let me show you the way.\n",ans);
int no=,x=,y=;
while(no<=ans)
{
int fx=path[x][y].x,fy=path[x][y].y;
if(map[fx][fy]) printf("%ds:(%d,%d)->(%d,%d)\n",no++,x,y,fx,fy);
for(int i=;i<=spec[fx][fy];i++) printf("%ds:FIGHT AT (%d,%d)\n",no++,fx,fy);
x=fx,y=fy;
}
}
printf("FINISH\n");
}
}
11872162 2014-10-14 19:47:07 Accepted 1026 15MS 552K 2286 B C++ Physcal

HDU 1026 (BFS搜索+优先队列+记录方案)的更多相关文章

  1. HDU 1242 (BFS搜索+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目大意:多个起点到一个终点,普通点耗时1,特殊点耗时2,求到达终点的最少耗时. 解题思路: ...

  2. HDU 2653 (记忆化BFS搜索+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2653 题目大意:迷宫中有普通点和陷阱.其中普通点可以走可以飞,但是陷阱只能飞.走耗时1,飞耗时2.但 ...

  3. hdu 1026 bfs+记录路径

    题意:从0,0点出发到n-1,m-1点,路上的数字代表要在这个点额外待多少秒,求最短的路 递归输出路径即可 #include<cstdio> #include<iostream> ...

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

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

  5. HDU 2531 (BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2531 题目大意: 你的身体占据多个点.每次移动全部的点,不能撞到障碍点,问撞到目标点块(多个点)的最 ...

  6. HDU 2612 (BFS搜索+多终点)

    题目链接: http://poj.org/problem?id=1947 题目大意:两人选择图中一个kfc约会.问两人到达时间之和的最小值. 解题思路: 对于一个KFC,两人的BFS目标必须一致. 于 ...

  7. HDU 1180 (BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1180 题目大意:迷宫中有一堆楼梯,楼梯横竖变化.这些楼梯在奇数时间会变成相反状态,通过楼梯会顺便到达 ...

  8. HDU 1312 (BFS搜索模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1312 题目大意:问迷宫中有多少个点被访问. 解题思路: DFS肯定能水过去的.这里就拍了一下BFS. ...

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

随机推荐

  1. 关于mac安装rails报错clang: error: unknown argument

    文章都是从我的个人博客上转载过来的,大家可以点击我的个人博客. www.iwangzheng.com mac上安装rails的时候报错, 安装rails的在终端执行了一句命令: $sudo gem i ...

  2. 重构edit 和 new页面

    www.iwangzheng.com 由于edit和new页面的相似部分很多,需要提取出来,现在就是提取的方法 从form 的开始部分选中,shift+v选中对应的行 :Rextract form 然 ...

  3. 转:PCL+VS2010环境配置

    1.下载 http://www.pointclouds.org/downloads/windows.html出下载PCL完全安装包1.6.0 all-in-one-installer,我的电脑是32位 ...

  4. BZOJ 3540 realtime-update 解题

    分析一下题意,大约是给定一串牛,然后找到一个跨越距离最长的牛子串使得在这个范围内白牛和花牛一样多. 白牛可以任意涂成花牛. 既然"白牛可以任意涂成花牛",那么我们需要找到一个最长的 ...

  5. mysql 多表连接

    现有表R,S如下: 笛卡尔积 select * from R,S; 结果: 注:不需要任何条件.结果为两张表函数相乘(3x3=9). 自连接 select e.empno,e.ename,m.empn ...

  6. 如何让你的scrapy爬虫不再被ban

    前面用scrapy编写爬虫抓取了自己博客的内容并保存成json格式的数据(scrapy爬虫成长日记之创建工程-抽取数据-保存为json格式的数据)和写入数据库(scrapy爬虫成长日记之将抓取内容写入 ...

  7. Android 中的异步下载

    网上提到最多的就是利用AsyncTask进行异步下载,用android-async-http第三方库的也比较多.这里写点注意事项. 先说说android-async-http,这个库发送请求利用thr ...

  8. hdu 1113 Word Amalgamation 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1113 题意:输入一个字典,然后再输入若干单词(每行中,1 <= 单词数 <= 100,并且 ...

  9. springMVC获取file,几种转换

    //从前台通过name值获取file MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)reque ...

  10. HDU 5742 It's All In The Mind (贪心) 2016杭电多校联合第二场

    题目:传送门. 题意:求题目中的公式的最大值,且满足题目中的三个条件. 题解:前两个数越大越好. #include <iostream> #include <algorithm> ...