发现直接搜索比较麻烦,但是要同时两个人一起走容易想到双向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. 白话 P-value

    准备再尝试一下,用大白话叙述一遍统计推断中最基础的东西(假设检验.P值.……),算是把这段时间的阅读和思考做个梳理(东西不难,思考侧重在如何表述和展示).这次打算用一种“迂回的”表达方式,比如,本文从 ...

  2. Mac系统存储-其他存储无故增大

    解决办法:打开Finder:安全倾倒废纸篓就会减少很大一部分存储.

  3. c语言字符串 数字转换函数大全

    最近学数据结构老是做实验 常用到字符串和数字的转换 想找却发现网上的资料太散 所以搜集整理一下 方便以后再用 atof(将字符串转换成浮点型数) atoi(将字符串转换成整型数) atol(将字符串转 ...

  4. storm源码剖析(2):storm的配置项

    storm的配置项,可以从backtype/storm/Config.java中找到所有配置项及其描述

  5. Confd 配置指导

    Quick Start Guide Before we begin be sure to download and install confd. Select a backend confd supp ...

  6. MySQL当月汇总 及负毛利汇总_20161027

    #当月汇总 及负毛利汇总 SELECT e.ID,e.city AS 城市 ,f.当月销售总额,f.当月成本总额,f.当月毛利总额,f.当月优惠券总额,f.当月赠品总额,f.当月毛利总额-f.当月优惠 ...

  7. poj 2559 最大矩形面积(单调栈)

    题目:输入一个整数n,代表有n个  1(宽度) * h[i](高度)的矩形.接下来n个数依次给定一个矩形高度的高度h[i](i<=n). 求:在给定的依次排列的这堆矩形构成的图形里用一个矩形圈出 ...

  8. 【Lintcode】106.Convert Sorted List to Balanced BST

    题目: Given a singly linked list where elements are sorted in ascending order, convert it to a height ...

  9. json格式化插件

    插件名称:JSON-Handle 下载地址: http://jsonhandle.sinaapp.com/

  10. C#窗体控件拖动

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...