发现直接搜索比较麻烦,但是要同时两个人一起走容易想到双向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 Ⅱ的更多相关文章

  1. HDU3085NightmareII题解--双向BFS

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3085 分析 大意就是一个男孩和一个女孩在网格里,同时还有两个鬼,男孩每轮走三步,女孩每轮走一步,与鬼曼 ...

  2. HDU3085(双向BFS+曼哈顿距离)题解

    Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  3. HDU 6171 Admiral(双向BFS+队列)题解

    思路: 最大步骤有20,直接BFS会超时. 因为知道开始情况和结果所以可以用双向BFS,每个BFS规定最大步骤为10,这样相加肯定小于20.这里要保存每个状态搜索到的最小步骤,用Hash储存.当发现现 ...

  4. HDU 3085 Nightmare Ⅱ 双向BFS

    题意:很好理解,然后注意几点,男的可以一秒走三步,也就是三步以内的都可以,鬼可以穿墙,但是人不可以,鬼是一次走两步 分析:我刚开始男女,鬼BFS三遍,然后最后处理答案,严重超时,然后上网看题解,发现是 ...

  5. UVA1601-The Morning after Halloween(双向BFS)

    Problem UVA1601-The Morning after Halloween Accept: 289 Submit: 3136 Time Limit: 12000 mSec  Problem ...

  6. 【bzoj5049】[Lydsy九月月赛]导航系统 并查集+双向BFS最短路

    题目描述 给你一张 $n$ 个点 $m$ 条边的随机图,边权为1.$k$ 次询问两点间最短路,不连通则输出-1. 输入 第一行包含3个正整数n,m,k(2<=n<=100000,1< ...

  7. HDU——1195Open the Lock(双向BFS)

    Open the Lock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  8. HDU3085 Nightmare Ⅱ —— 双向BFS + 曼哈顿距离

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Other ...

  9. CSUOJ2031-Barareh on Fire(双向BFS)

    Barareh on Fire Submit Page Description The Barareh village is on fire due to the attack of the virt ...

随机推荐

  1. Spring Boot2.0之整合事物管理

    首先Spring 事务分类 1.声明事务  原理:基于编程事务的 2.编程事务  指定范围 扫包去解决 3.事务原理:AOP技术   通过环绕通知进行了拦截 使用Spring 事务注意事项: 不要tr ...

  2. Contiki Process概述

    本文涉及到的Protothread机制知识,在http://www.cnblogs.com/songdechiu/p/5793717.html 一.进程类型 进程类型主要有协同式(cooperativ ...

  3. smokeping 微信报警配置

    1. 准备alert脚本,用来调用微信脚本 #!/bin/bash alertname=$ target=$ losspattern=$ rtt=$ smokename="hq_to_idc ...

  4. PYTHON 爬虫笔记五:BeautifulSoup库基础用法

    知识点一:BeautifulSoup库详解及其基本使用方法 什么是BeautifulSoup 灵活又方便的网页解析库,处理高效,支持多种解析器.利用它不用编写正则表达式即可方便实现网页信息的提取库. ...

  5. html5--2.1新的布局元素(1)-header/footer

    html5--2.1新的布局元素(1)-header/footer 学习要点 了解header/footer的语义和用法 使用header/footer进行一个简单的布局 header元素(标签) 用 ...

  6. Gym - 101673:B Craters (几何,求凸包)

    题意:给定几个圆,求最短的围合,把这几个包围起来,而且到圆的距离都不小于10. 思路:把每个圆的半径+10,边等分5000份,然后求凸包即可. #include<bits/stdc++.h> ...

  7. ACM学习历程—HDU1003 Max Sum(dp && 最大子序列和)

    Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub ...

  8. [转]深入了解 CSS3 新特性

    简介 CSS 即层叠样式表(Cascading Stylesheet).Web 开发中采用 CSS 技术,可以有效地控制页面的布局.字体.颜色.背景和其它效果.只需要一些简单的修改,就可以改变网页的外 ...

  9. 网络最大流dinic模板

    #include<iostream> #include<cstdio> #include<cstring> #include<queue> using ...

  10. 用于获取或设置Web.config/*.exe.config中节点数据的辅助类

    1. 用于获取或设置Web.config/*.exe.config中节点数据的辅助类 /**//// <summary> /// 用于获取或设置Web.config/*.exe.confi ...