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 ...
随机推荐
- 【XSY2534】【CF835D】Palindromic characteristics 回文自动机
题目大意 一个字符串\(s\)是\(1\)−回文串当且仅当这个串是回文串. 一个串\(s\)是\(k\)−回文串\((k>1)\)当且仅当\(s\)的前一半与后一半相同且\(s\)的前一 ...
- 普通Splay详解
预备知识: 二叉搜索树(BST) 至于BST,随便看一下就可以, 我们知道二叉搜索树是O(logN)的,那我们为什么要用平衡树呢? 之前我们了解到,BST的插入是小的往左子树走,大的往右子树走,如果凉 ...
- MT【291】2元非齐次不等式
实数$x,y$满足$x^2+y^2=20,$求$xy+8x+y$的最大值___ 法一:$xy\le\dfrac{1}{4}x^2+y^2,8x\le x^2+16,y\le\dfrac{1}{4}y^ ...
- 【BZOJ2324】[ZJOI2011]营救皮卡丘(网络流,费用流)
[BZOJ2324][ZJOI2011]营救皮卡丘(网络流,费用流) 题面 BZOJ 洛谷 题解 如果考虑每个人走的路径,就会很麻烦. 转过来考虑每个人破坏的点集,这样子每个人可以得到一个上升的序列. ...
- django从零开始-视图
1.处理的登录请求 views文章中添加登录函数login_action def login_action(request): if request.method == 'POST': usernam ...
- LVS搭建负载均衡(一)NAT模型
应用场景:LVS配置负载均衡方式之一:nat 测试环境: 测试步骤: 1. 在主机lvs上安装ipvsadm lvs~]# yum install ipvsadm -y lvs~]# ipvsadm ...
- 买卖股票的最佳时机II
题目描述 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润. 注意你不能在买入股票前卖出股 ...
- OpenLayers学习笔记(二)— QML与HTML通信之画图
作者: 狐狸家的鱼 Github: 八至 本文链接:QML与 HTML通信—实现QML中点击功能按钮在地图上画图 一.HTML-map var drarGraphic; var drawType;fu ...
- Electron入门笔记(一)-自己快速搭建一个app demo
Electron学习-快速搭建app demo 作者: 狐狸家的鱼 Github: 八至 一.安装Node 1.从node官网下载 ,最好安装.msi后缀名的文件,新手可以查看安装教程进行安装. 2. ...
- windows蜜汁调音
哈,用的蜂鸣器,我静音了这东西还放. 只能调的很垃圾,但是很好玩. #include<cstdio> #include<windows.h> using namespace s ...