HDU 1026 (BFS搜索+优先队列+记录方案)
题目链接: 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搜索+优先队列+记录方案)的更多相关文章
- HDU 1242 (BFS搜索+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目大意:多个起点到一个终点,普通点耗时1,特殊点耗时2,求到达终点的最少耗时. 解题思路: ...
- HDU 2653 (记忆化BFS搜索+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2653 题目大意:迷宫中有普通点和陷阱.其中普通点可以走可以飞,但是陷阱只能飞.走耗时1,飞耗时2.但 ...
- hdu 1026 bfs+记录路径
题意:从0,0点出发到n-1,m-1点,路上的数字代表要在这个点额外待多少秒,求最短的路 递归输出路径即可 #include<cstdio> #include<iostream> ...
- hdu 1026(BFS+输出路径) 我要和怪兽决斗
http://acm.hdu.edu.cn/showproblem.php?pid=1026 模拟一个人走迷宫,起点在(0,0)位置,遇到怪兽要和他决斗,决斗时间为那个格子的数字,就是走一个格子花费时 ...
- HDU 2531 (BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2531 题目大意: 你的身体占据多个点.每次移动全部的点,不能撞到障碍点,问撞到目标点块(多个点)的最 ...
- HDU 2612 (BFS搜索+多终点)
题目链接: http://poj.org/problem?id=1947 题目大意:两人选择图中一个kfc约会.问两人到达时间之和的最小值. 解题思路: 对于一个KFC,两人的BFS目标必须一致. 于 ...
- HDU 1180 (BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1180 题目大意:迷宫中有一堆楼梯,楼梯横竖变化.这些楼梯在奇数时间会变成相反状态,通过楼梯会顺便到达 ...
- HDU 1312 (BFS搜索模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1312 题目大意:问迷宫中有多少个点被访问. 解题思路: DFS肯定能水过去的.这里就拍了一下BFS. ...
- 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 ...
随机推荐
- python4delphi Python could not be properly initialized. We must quit.
要用32位的DLL,不要用64位的dll Unable to load Python 2.7 dll with Delphi 2010 #6 Closed GoogleCodeExporter op ...
- Nikto是一款Web安全扫描工具,可以扫描指定主机的web类型,主机名,特定目录,cookie,特定CGI漏洞,XSS漏洞,SQL注入漏洞等,非常强大滴说。。。
Nikto是一款Web安全扫描工具,可以扫描指定主机的web类型,主机名,特定目录,cookie,特定CGI漏洞,XSS漏洞,SQL注入漏洞等,非常强大滴说... root@xi4ojin:~# cd ...
- EtherCAT报文寻址
EtherCAT通信通过主站发送EtherCAT数据帧读写从站设备的内部存储区实现. 一个EtherCAT网段相当于一个以太网设备,主站首先通过以太网数据帧头的MAC地址寻址到网段,然后使用Ether ...
- OpenStack
[官网]http://www.openstack.org/ [视频教程1]http://blog.csdn.net/u010973404/article/details/16841229 [视频教程2 ...
- Expected MultipartHttpServletRequest: is a MultipartResolver configured?
2015-05-05 19:09:47.510::WARN: /purchase/long-term-contract/uploading.htmjava.lang.IllegalArgumentEx ...
- [Android Pro] 监听WIFI 打开广播
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-perm ...
- glut编译问题 (程序无法运行)
参考:http://blog.csdn.net/robinjwong/article/details/5636049 error: the procedure entry point _glutini ...
- Mac相关命令
1,查询端口占用与Kill相应进程 lsof -i:端口,查询端口的占用情况 kill PID,关闭指定PID的进程. 如: localhost:~ tianjingcheng$ kill 729 l ...
- C++基础内容复习
下列语句定义了5个变量: int count; double sales_price,sum; std::string title; Sales_item bookItem; 每个定义都是以类型说明符 ...
- CodeIgniter - 数据库的增删改查
数据库操作无非是CRUD,用非装逼的语言来说就是增删改查.也许这一节会讲的很泛泛,或者很多人看不懂,没关系,大致的看看,知道是这么回事就好,继续往后看,后面会讲实例,这些枯燥而又抽象的东西可以先跳过, ...