题目链接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. 如何改变服务器的本地域名来访问本地服务器 而不用localhost或者127.0.0.1来访问

    1. vim nginx.conf 如下: server {                listen       80;                server_name pma;       ...

  2. C++中的vector使用范例

    原文链接 http://blog.csdn.net/tjh666/article/details/1604119 1.vector 的数据的存入和输出: #include<stdio.h> ...

  3. mysql数据库性能优化(包括SQL,表结构,索引,缓存)

    优化目标减少 IO 次数IO永远是数据库最容易瓶颈的地方,这是由数据库的职责所决定的,大部分数据库操作中超过90%的时间都是 IO 操作所占用的,减少 IO 次数是 SQL 优化中需要第一优先考虑,当 ...

  4. win10 x64下安装oracle 12c出现[INS-30131]报错的解决方案

    解决方案: 第一步:控制面板>所有控制面板项>管理工具>服务>SERVER 启动 第二步:控制面板>所有控制面板项>管理工具>计算机管理>系统工具> ...

  5. 【GoLang】GoLang GOPATH 工程管理 最佳实践

    参考资料: MAC下 Intellij IDEA GO语言插件安装及简单案例:http://blog.csdn.net/fenglailea/article/details/53054502 关于wi ...

  6. 【OpenStack】OpenStack系列4之Glance详解

    下载安装 参考:http://www.linuxidc.com/Linux/2012-08/68964.htm http://www.it165.net/os/html/201402/7246.htm ...

  7. php-fpm 进程管理

    2015年2月26日 15:40:15 先查找 PHP-FPM 的进程号 ps -ef | grep php-fpm root Feb12 ? :: php-fpm: master process ( ...

  8. Java性能优化权威指南-读书笔记(三)-JVM性能调优-内存占用

    新生代.老年代.永久代的概念不多说,这三个空间中任何一个不能满足内存分配请求时,就会发生垃圾收集. 新生代不满足内存分配请求时,发生Minor GC,老年代.永久代不满足内存分配请求时,发生Full ...

  9. Java性能优化权威指南-读书笔记(一)-操作系统性能监控工具

    一:CPU 1. 用户态CPU是指执行应用程序代码的时间占总CPU时间的百分比. 系统态CPU是指应用执行操作系统调用的时间占总CPU时间的百分比.系统态CPU高意味着共享资源有竞争或者I/O设备之间 ...

  10. struts1老古董配置

    <!--Struts1 struts-config.xml Demo --><?xml version="1.0" encoding="UTF-8&qu ...