HDU 1026(迷宫 BFS+打印)
题意是要穿过一个迷宫并且将每一步打印出来。
用宽搜的方法找到路径,在 vis 中存一下方向,只是这题被看到的一种不太对的运算符重载坑了很久......
代码如下:
#include <bits/stdc++.h>
using namespace std;
const int maxn = ;
char mp[maxn][maxn];
int n,m,t,cnt,vis[maxn][maxn],fight[maxn][maxn];
int dir[][] = {,,,,-,,,-};
struct node
{
int x,y,time;
bool operator<(const node &a) const {
return a.time<time;
}
// 不能用的重载
// friend bool operator < (node a,node b)
// {
// return a.time < b.time;
// }
};
//struct cmp
//{
// bool operator () (node a,node b)
// {
// return a.time < b.time;
// }
//};
bool bfs()
{
priority_queue<node> q;
// priority_queue<node,vector<node>,cmp> q;
node st;
st.x = st.y = st.time = ;
q.push(st);
mp[][] = 'X';
while (!q.empty())
{
node cur = q.top();
q.pop();
if(cur.x==n- && cur.y==m-)
{
t = cur.time;
return true;
}
for(int i = ; i < ; ++i)
{
node next;
next.x = cur.x+dir[i][];
next.y = cur.y+dir[i][];
if(mp[next.x][next.y]=='X') continue;
if(next.x<n && next.x>= && next.y<m && next.y>= )
{
if(mp[next.x][next.y]!='.')
{
next.time = cur.time++mp[next.x][next.y]-'';
fight[next.x][next.y] = mp[next.x][next.y]-'';
}
else next.time = cur.time+;
vis[next.x][next.y] = i+;
q.push(next);
mp[next.x][next.y] = 'X';
}
}
}
return false;
}
void prtmp(int x,int y)
{
if(vis[x][y] == ) return ;
int prex,prey;
prex = x - dir[vis[x][y]-][];
prey = y - dir[vis[x][y]-][];
prtmp(prex,prey);
cout << (cnt++) << "s:(" << prex << "," << prey << ")->(" << x << "," << y << ")\n";
while(fight[x][y]--)
cout << (cnt++) << "s:FIGHT AT (" << x << "," << y << ")\n";
}
int main()
{
while(cin >> n >> m)
{
memset(fight,,sizeof(fight));
memset(vis,,sizeof(vis));
for(int i = ; i < n; ++i)
for(int j = ; j < m; ++j)
cin >> mp[i][j];
if(bfs())
{
cnt = ;
cout << "It takes " << t << " seconds to reach the target position, let me show you the way.\n";
prtmp(n-,m-);
}
else cout << "God please help our poor hero.\n";
cout << "FINISH\n";
}
return ;
}
HDU 1026(迷宫 BFS+打印)的更多相关文章
- HDU 1026
http://acm.hdu.edu.cn/showproblem.php?pid=1026 记录bfs路径,用一个数组记录next前驱的方向,然后递归的时候减回去即可 #include <io ...
- HDU 1269 迷宫城堡(强连通)
HDU 1269 迷宫城堡 pid=1269" target="_blank" style="">题目链接 题意:中文题 思路:强连通模板题 代 ...
- ZOJ 1649 Rescue(有敌人迷宫BFS)
题意 求迷宫中从a的位置到r的位置须要的最少时间 经过'.'方格须要1s 经过'x'方格须要两秒 '#'表示墙 因为有1s和2s两种情况 须要在基础迷宫bfs上加些推断 令到达每一个点的时间初 ...
- HDU 1026 Ignatius and the Princess I(带路径的BFS)
http://acm.hdu.edu.cn/showproblem.php?pid=1026 题意:给出一个迷宫,求出到终点的最短时间路径. 这道题目在迷宫上有怪物,不同HP的怪物会损耗不同的时间,这 ...
- HDU 1026 (BFS搜索+优先队列+记录方案)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 题目大意:最短时间内出迷宫.迷宫里要杀怪,每个怪有一定HP,也就是说要耗一定时.输出方案. 解 ...
- hdu 1026(BFS+输出路径) 我要和怪兽决斗
http://acm.hdu.edu.cn/showproblem.php?pid=1026 模拟一个人走迷宫,起点在(0,0)位置,遇到怪兽要和他决斗,决斗时间为那个格子的数字,就是走一个格子花费时 ...
- hdu 1026 Ignatius and the Princess I (bfs+记录路径)(priority_queue)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1026 Problem Description The Princess has been abducted ...
- hdu 1026 Ignatius and the Princess I【优先队列+BFS】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- hdu 1728 逃离迷宫 bfs记转向
题链:http://acm.hdu.edu.cn/showproblem.php?pid=1728 逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) Mem ...
随机推荐
- require.js基本用法
1.require.js的加载 使用require.js的第一步,是先去官方网站下载最新版本. 下载后,假定把它放在js子目录下面,就可以加载了. 1 <script src="js/ ...
- HTML head标签内部常用设置
HTML head标签内部常用设置 在网页html文件中,head标签里面通常放置的代码是用来对网页进行相关设置的内容.下面就是对这些内容的介绍. meta标签的设置 在网页中,meta标签最常用的设 ...
- 【CF1009F】Dominant Indices(长链剖分)
[CF1009F]Dominant Indices(长链剖分) 题面 洛谷 CF 翻译: 给定一棵\(n\)个点,以\(1\)号点为根的有根树. 对于每个点,回答在它子树中, 假设距离它为\(d\)的 ...
- gulp与webpack的区别
gulp gulp强调的是前端开发的工作流程,我们可以通过配置一系列的task,定义task处理的事务(例如文件压缩合并.雪碧图.启动server.版本控制等),然后定义执行顺序,来让gulp执行这 ...
- 学习笔记:fhq-treap
0. 前置知识:\(treap\)的定义 树堆,在数据结构中也称Treap,是指有一个随机附加域满足堆的性质的二叉搜索树,其结构相当于以随机数据插入的二叉搜索树. >--摘自百度百科 形象化 ...
- BZOJ 1854: [Scoi2010]游戏(二分图匹配/并查集)
题面: https://www.lydsy.com/JudgeOnline/problem.php?id=1854 题解: 1.二分图匹配: 首先我们发现每件装备只能在两种属性中选一种.因此,我们以每 ...
- 洛谷 P1393 P3157 动态逆序对
嘛,好久没碰CDQ分治了,做道题练练手. 时间倒流——把删数改为加数. 对于每个被删的,我的想法是拆成询问和add,后来发现一个足矣. 我本来准备对每个删的数都求一遍整体逆序对,后来发现无论如何都不可 ...
- (转)CDN的作用与基本过程
背景:积累大型网站开发中需要掌握的技术. CDN的作用与基本过程 https://blog.csdn.net/lihao21/article/details/52808747#comments CDN ...
- (三)orcale体系结构
https://www.cnblogs.com/chengxiao/p/5904783.html 一.什么是Oracle数据库? 众所周知,Oracle DataBase是一款关系型数据库管理系统(不 ...
- SpringBoot+Shiro入门小栗子
写一个不花里胡哨的纯粹的Springboot+Shiro的入门小栗子 效果如图: 首页:有登录注册 先注册一个,然后登陆 登录,成功自动跳转到home页 home页:通过认证之后才可以进 代码部分: ...