题目链接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. DICOM医学图像处理:storescp.exe与storescu.exe源码剖析,学习C-STORE请求

    转载:http://blog.csdn.net/zssureqh/article/details/39213817 背景: 上一篇专栏博文中针对PACS终端(或设备终端,如CT设备)与RIS系统之间w ...

  2. ubuntu14.04安装chrome

    到https://www.google.com/chrome/browser/desktop/index.html可下载指定版本的deb文件. 32bit: wget https://dl.googl ...

  3. 【GoLang】GoLang 微服务、开源库等参考资料

    参考资料: GoLang书籍: https://github.com/dariubs/GoBooksGo名库: https://github.com/Unknwon/go-rock-libraries ...

  4. Java中成员变量和局部变量的区别

    java面向对象过程中,最基本的两类变量就是成员变量和局部变量 成员变量是写在类中并且写在方法外部,一般写在每个类的头部,用于初始化或者方法操作,作用域是整个类被实例化到被销毁,中间变量都可以被外部方 ...

  5. Java for LeetCode 067 Add Binary

    Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...

  6. byte[]和InputStream的相互转换[转载]

    1:byte[]转换为InputStream InputStream sbs = new ByteArrayInputStream(byte[] buf); 2:InputStream转换为Input ...

  7. curl请求的url中含有空格

    curl请求的url中含有空格时(例如rul的参数是sql查询语句,url=www.tets.com/query.php?sql=select * from t1),curl_easy_perform ...

  8. Fence Repair(poj 3253)

    题意: 有一个农夫要把一个木板钜成几块给定长度的小木板,每次锯都要收取一定费用,这个费用就是当前锯的这个木版的长度 给定各个要求的小木板的长度,及小木板的个数n,求最小费用 提示: 以 3 5 8 5 ...

  9. 考前复习(codevs 2837)

    2837 考前复习  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description Aiden马上要考试了,可他 ...

  10. Maven类包冲突终极解决方案

    本文转自:http://ian.wang/106.htm 举例A依赖于B及C,而B又依赖于X.Y,而C依赖于X.M,则A除引B及C的依赖包下,还会引入X,Y,M的依赖包(一般情况下了,Maven可通过 ...