Nightmare Ⅱ

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 720    Accepted Submission(s): 136

Problem Description
Last night, little erriyue had a horrible nightmare. He dreamed that he and his girl friend were trapped in a big maze separately. More terribly, there are two ghosts in the maze. They will kill the people. Now little erriyue wants to know if he could find his girl friend before the ghosts find them.
You may suppose that little erriyue and his girl friend can move in 4 directions. In each second, little erriyue can move 3 steps and his girl friend can move 1 step. The ghosts are evil, every second they will divide into several parts to occupy the grids within 2 steps to them until they occupy the whole maze. You can suppose that at every second the ghosts divide firstly then the little erriyue and his girl friend start to move, and if little erriyue or his girl friend arrive at a grid with a ghost, they will die.
Note: the new ghosts also can devide as the original ghost.
 
Input
The input starts with an integer T, means the number of test cases.
Each test case starts with a line contains two integers n and m, means the size of the maze. (1<n, m<800)
The next n lines describe the maze. Each line contains m characters. The characters may be:
‘.’ denotes an empty place, all can walk on.
‘X’ denotes a wall, only people can’t walk on.
‘M’ denotes little erriyue
‘G’ denotes the girl friend.
‘Z’ denotes the ghosts.
It is guaranteed that will contain exactly one letter M, one letter G and two letters Z. 
 
Output
Output a single integer S in one line, denotes erriyue and his girlfriend will meet in the minimum time S if they can meet successfully, or output -1 denotes they failed to meet.
 
Sample Input
 

3
5 6
XXXXXX
XZ..ZX
XXXXXX
M.G...
......
5 6
XXXXXX
XZZ..X
XXXXXX
M.....
..G...

10 10
..........
..X.......
..M.X...X.
X.........
.X..X.X.X.
.........X
..XX....X.
X....G...X
...ZX.X...
...Z..X..X

Sample Output
1
1
-1
 
Author
二日月
 
Source
 
注意的地方
1:鬼可以穿过墙。
2:每一步鬼要先走,如果发现此事G,M被覆盖了,说明,呵呵,他(她)走不了。
   You can suppose that at every second the ghosts divide firstly.
   不然第三组测试数据,不好解释。
3.关于M的三步,很显然,如果前一步走不了,就不能在从这一点继续走下去了。
   就是要注意,如果走一步的时候不能走,那么就不能在这个点基础上又走一步。 
 #include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<queue>
using namespace std; int n,m,step;
int zx1,zy1,zx2,zy2;
int mx,my,gx,gy;
int to[][]={{,},{,},{,-},{-,}};
char a[][];
bool hash[][][],glag;
struct node
{
int x,y;
};
queue<node>Q[]; int Min(int x,int y)
{
return x>y? y:x;
}
bool fun(node &t)
{
int one,two;
if(t.x>=&&t.x<=n && t.y>=&&t.y<=m && a[t.x][t.y]!='X')
{
one=abs(t.x-zx1)+abs(t.y-zy1);
two=abs(t.x-zx2)+abs(t.y-zy2);
one=(one+)/;
two=(two+)/;
one=Min(one,two);
if(one>step) return false;
}
return true;
}
void bfs(int x)
{
int i,size1;
node t,cur;
size1=Q[x].size();
while(size1--)
{
cur=Q[x].front();
Q[x].pop();
if(fun(cur))continue;
for(i=;i<;i++)
{
t=cur;
t.x=t.x+to[i][];
t.y=t.y+to[i][];
if(fun(t))continue;
if(hash[x][t.x][t.y])continue;
hash[x][t.x][t.y]=true;
if(hash[x^][t.x][t.y])
{
glag=true;
return;
}
Q[x].push(t);
}
}
}
void dbfs()
{
node t;
t.x=mx;
t.y=my;
hash[][t.x][t.y]=true;
Q[].push(t);//boy
t.x=gx;
t.y=gy;
hash[][t.x][t.y]=true;
Q[].push(t);//girl
step=;
glag=false;
while(true)
{
if(Q[].size()== && Q[].size()==)break;
step++;
bfs();
bfs();
bfs();
bfs();
if(glag==true)break;
}
if(glag==true)printf("%d\n",step);
else printf("-1\n");
}
void solve()
{
memset(hash,false,sizeof(hash));
while(!Q[].empty()){
Q[].pop();
}
while(!Q[].empty()){
Q[].pop();
}
dbfs();
}
int main()
{
int T;
bool flag;
int i,j;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(i=;i<=n;i++)
scanf("%s",a[i]+);
for(i=,flag=false;i<=n;i++)
for(j=;j<=m;j++)
{
if(a[i][j]=='Z' && !flag){
zx1=i;
zy1=j;
flag=true;
}
else if(a[i][j]=='Z' && flag){
zx2=i;
zy2=j;
}
if(a[i][j]=='M'){
mx=i;
my=j;
}
if(a[i][j]=='G'){
gx=i;
gy=j;
}
}
solve();
}
return ;
}

hdu 3085的更多相关文章

  1. HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ)

    HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  2. HDU - 3085 Nightmare Ⅱ

    HDU - 3085 Nightmare Ⅱ 双向BFS,建立两个队列,让男孩女孩一起走 鬼的位置用曼哈顿距离判断一下,如果该位置与鬼的曼哈顿距离小于等于当前轮数的两倍,则已经被鬼覆盖 #includ ...

  3. HDU 3085 Nightmare II 双向bfs 难度:2

    http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...

  4. HDU 3085 Nightmare Ⅱ(双向BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 题目大意:给你一张n*m地图上,上面有有 ‘. ’:路 ‘X':墙 ’Z':鬼,每秒移动2步,可 ...

  5. hdu 3085(双向bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 思路:双向广搜,每次从M出发,搜三步,从G出发,搜一步,然后就是判断是否走到对方已经走过的格子, ...

  6. 【HDU 3085】 Nightmare Ⅱ

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3085 [算法] 双向BFS [代码] #include<bits/stdc++.h> ...

  7. HDU 3085 Nightmare Ⅱ (双向BFS)

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

  8. HDU 3085 Nightmare Ⅱ 双向BFS

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

  9. HDU - 3085 双向BFS + 技巧处理 [kuangbin带你飞]专题二

    题意:有两只鬼,一个男孩女孩被困在迷宫中,男孩每秒可以走三步,女孩只能1步,鬼可以两步且可以通过墙.问男孩女孩是否可以在鬼抓住他们之前会合? 注意:每秒开始鬼先移动,然后两人开始移动. 思路:以男孩和 ...

随机推荐

  1. day 82 Django Admin组件.

    一.先建表环境 modules文件 from django.db import models # Create your models here. from django.contrib.auth.m ...

  2. Day44 数据库的操作

    视图操作: 1.左连接查询 select * from person left join dept on person.dept_id = dept.did 2. 右连接 3. 内连接  inner ...

  3. 修改tomcat7编码问题(重定向等)

    修改tomcat默认编码格式: 修改tomcat下的conf/server.xml文件,找到如下代码: <Connector port="8080" protocol=&qu ...

  4. docker 下载加速

    执行这个命令: curl -SSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud. ...

  5. JVM之JIT

    JIT技术是JVM中最重要的核心模块之一.我的课程里本来没有计划这一篇,但因为不断有朋友问起,Java到底是怎么运行的?既然Hotspot是C++写的,那Java是不是可以说运行在C++之上呢?为了澄 ...

  6. php获取客户端ip地址方法

    /** * 获取客户端IP地址 * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字 * @param boolean $adv 是否进行高级模式获取(有 ...

  7. 对Deeplung检测+两样性分类的一个整合

    整体的流程分为以下几步: 读取原始数据(.mhd文件)——> 生成mask ——> 对数据预处理 ——> 执行检测 ——> 对检测结果进行分类 ——>可视化 懒一点,不贴 ...

  8. (转)python 判断数据类型

    原文:https://blog.csdn.net/mydriverc2/article/details/78687269 Python 判断数据类型有type和isinstance 基本区别在于: t ...

  9. Android 手势识别—缩放

    上一篇讲解了手势识别中的点击和双击事件的识别,用到的是GestureDetector类和GestureDetectorCompat类,用于监听用户触摸屏幕中的简单动作. 缩放 基本用法如下,可以通过缩 ...

  10. Win7下无法提交MapReduce Job到集群环境(转)

    一. 对hadoop eclipse plugin认识不足 http://zy19982004.iteye.com/blog/2024467曾经说到我最hadoop eclipse plugin作用的 ...