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. 740. Delete and Earn

    Given an array nums of integers, you can perform operations on the array. In each operation, you pic ...

  2. ZZNU 1719(最长上升子序列+最长下降子序列)

    先吐血一发,噗! 再吐血一次,啊啊啊啊! 好吧,做了那么多次最长上升子序列,看这题看了半天才发现还有最长下降子序列,呵呵哒! AC代码: #include<stdio.h>//老恶心#in ...

  3. 快速滑动时 `cellForRow` 的调用次数

    问题 有一个 1000 个 cell 的 tableView,刚刚进入界面时,contentOffset 为 0.用手快速滑动 tableView,直至最下面一个 cell 显示在屏幕上. 这个过程中 ...

  4. linux文件的硬连接和软连接

    建立软连接:ln -s 原路径 目标路径 原理示意图: 特点: 1.     相当于win中的快捷方式 2.     删除链接文件,源文件不受影响 3.     删除源文件,链接文件失效 4.     ...

  5. 从负数开始 ,跟随别大人脚步 ---java

    刚刚毕业    音乐生  目前在做 数据库测试和实施的相关工作 . 1个月前认识了别大人  , 打算边工作 ,边学习java 开启学习之路 . ..340多个G的java视频感觉解压完1T  足够我喝 ...

  6. php判断是否使用手机访问

    直接上代码 /** * 检测是否使用手机访问 * @access public * @return bool */ public function isMobile() { if (isset($_S ...

  7. Neko and Aki's Prank CodeForces - 1152D (括号序列,dp)

    大意: 将所有长度为2*n的合法括号序列建成一颗trie树, 求trie树上选出一个最大不相交的边集, 输出边集大小. 最大边集数一定不超过奇数层结点数. 这个上界可以通过从底层贪心达到, 所以就转化 ...

  8. EJB3 EntityBean中EntityManager的管理类型

    EJB中EntityManager的管理方式有两种:Container-managed EntityManager和Application-managed EntityManager 即容器管理的En ...

  9. Java之集合(十六)ArrayBlockingQueue

    转载请注明源出处:http://www.cnblogs.com/lighten/p/7427763.html 1.前言 JDK5是一个重要的更新版本,其提供了大量的并发类.之前的介绍都是一些util下 ...

  10. Java学习之路(十):异常

    ---恢复内容开始--- 异常的概述和分类 Throwable类是Java语言中所有错误或者异常的超类(也就是说,Java中所有的报错都是继承与Throwable的),也只有当对象是此类或者此类的子类 ...