[题解](双向bfs)hdu_3085_Nightmare Ⅱ
发现直接搜索比较麻烦,但是要同时两个人一起走容易想到双向bfs,比较普通,
在判断是否碰到ghost时只要比较两点的曼哈顿距离大小和step*2(即ghost扩散的距离)即可,仔细思考也是可以想到的
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn=;
struct node{
int x,y;
node(){}
node(int a,int b){
x=a,y=b;
}
}mon[];
queue<node>pq[];
int n,m;
char maze[maxn][maxn];
int used[][maxn][maxn];
int dx[]={,-,,};
int dy[]={,,,-};
int step;
int check(node a){
if(a.x< || a.y< || a.x>=n || a.y>=m)return ;
if(maze[a.x][a.y]=='X')return ;
if((abs(a.x-mon[].x)+abs(a.y-mon[].y))<=*step)return ;
if((abs(a.x-mon[].x)+abs(a.y-mon[].y))<=*step)return ;
return ;
}
int bfs(int w){
// while(!pq[w].empty()){
int sum = pq[w].size();//这里不能搜完
while (sum--){
int x=pq[w].front().x,y=pq[w].front().y;pq[w].pop();
if(!check(node(x,y)))continue;
for(int i=;i<;i++){
int xx=x+dx[i],yy=y+dy[i];
if(!check(node(xx,yy)))continue;
if(!used[w][xx][yy]){
if(used[w^][xx][yy]==)return ;
used[w][xx][yy]=;
pq[w].push(node(xx,yy));
}
}
}
return ;
}
int ax,ay,bx,by;
int solve(){
while(!pq[].empty())pq[].pop();
while(!pq[].empty())pq[].pop();
pq[].push(node(ax,ay));
pq[].push(node(bx,by));
memset(used,,sizeof(used));
used[][ax][ay]=used[][bx][by]=;
step=;
while(!pq[].empty() || !pq[].empty()){
step++;
if(bfs()==)return step;
if(bfs()==)return step;
if(bfs()==)return step;
if(bfs()==)return step;
}
return -;
}
int main(){int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
int cnt=;
for(int i=;i<n;i++)scanf("%s",maze[i]);
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(maze[i][j]=='G')
bx=i,by=j;
else if(maze[i][j]=='M')
ax=i,ay=j;
else if(maze[i][j]=='Z')
mon[cnt].x=i,mon[cnt++].y=j;
}
}
printf("%d\n",solve());
} }
[题解](双向bfs)hdu_3085_Nightmare Ⅱ的更多相关文章
- HDU3085NightmareII题解--双向BFS
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3085 分析 大意就是一个男孩和一个女孩在网格里,同时还有两个鬼,男孩每轮走三步,女孩每轮走一步,与鬼曼 ...
- HDU3085(双向BFS+曼哈顿距离)题解
Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 6171 Admiral(双向BFS+队列)题解
思路: 最大步骤有20,直接BFS会超时. 因为知道开始情况和结果所以可以用双向BFS,每个BFS规定最大步骤为10,这样相加肯定小于20.这里要保存每个状态搜索到的最小步骤,用Hash储存.当发现现 ...
- HDU 3085 Nightmare Ⅱ 双向BFS
题意:很好理解,然后注意几点,男的可以一秒走三步,也就是三步以内的都可以,鬼可以穿墙,但是人不可以,鬼是一次走两步 分析:我刚开始男女,鬼BFS三遍,然后最后处理答案,严重超时,然后上网看题解,发现是 ...
- UVA1601-The Morning after Halloween(双向BFS)
Problem UVA1601-The Morning after Halloween Accept: 289 Submit: 3136 Time Limit: 12000 mSec Problem ...
- 【bzoj5049】[Lydsy九月月赛]导航系统 并查集+双向BFS最短路
题目描述 给你一张 $n$ 个点 $m$ 条边的随机图,边权为1.$k$ 次询问两点间最短路,不连通则输出-1. 输入 第一行包含3个正整数n,m,k(2<=n<=100000,1< ...
- HDU——1195Open the Lock(双向BFS)
Open the Lock Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDU3085 Nightmare Ⅱ —— 双向BFS + 曼哈顿距离
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Other ...
- CSUOJ2031-Barareh on Fire(双向BFS)
Barareh on Fire Submit Page Description The Barareh village is on fire due to the attack of the virt ...
随机推荐
- 51Nod 1294 修改数组 —— LIS
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1294 1294 修改数组 题目来源: HackerRank ...
- WebStorm中SVN配置
近期在使用WebStorm进行网页开发,值得一提的是WebStorm的确是一个不错的IDE,尽管可能内存开销较大,但是在编写JS的时候提供了很多包括自动完成等强大的功能. 好了,步入正题:在实际项目开 ...
- Apache-POI 简单应用
测试的Excel文件为四列的普通表格 jar包:poi-3.15-beta2.jar(Office2003xls文件).poi-ooxml-3.15-beta2.jar(Office2007xlsx文 ...
- not in和not exist的区别(转)
in和exists in 是把外表和内表作hash 连接,而exists是对外表作loop循环,每次loop循环再对内表进行查询.一直以来认为exists比in效率高的说法是不准确的. 如果查询的两个 ...
- hdu 6006
HDU - 6006 Engineer Assignment 我参考了这份题解. 贴上我比较拙的代码,留念一下. /** * 想到状态压缩的dp问题就解决了一半. */ #include <st ...
- u3d 多线程 网络
开启一个线程做网络连接,和接收数据, 用event进行广播 using UnityEngine; using System; using System.Threading; using System. ...
- 「P4994」「洛谷11月月赛」 终于结束的起点(枚举
题目背景 终于结束的起点终于写下句点终于我们告别终于我们又回到原点…… 一个个 OIer 的竞赛生涯总是从一场 NOIp 开始,大多也在一场 NOIp 中结束,好似一次次轮回在不断上演.如果这次 NO ...
- 【Lintcode】177.Convert Sorted Array to Binary Search Tree With Minimal Height
题目: Given a sorted (increasing order) array, Convert it to create a binary tree with minimal height. ...
- DS:架构-1
ylbtech-DS:架构-1 1. 类库返回顶部 1. 2. 2. 引用返回顶部 1. Api-Base\Common\OAuth2Provider\ServiceBase-OAuth2Provid ...
- Repeater 和 GridView 添加序列号
<tr><asp:Repeater ID="rptOfBrowerInfo" runat="server" > <Heade ...