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. Exp4 恶意代码分析 20164321 王君陶

    Exp4 恶意代码分析 20164321 王君陶 1.实践目标 1.1是监控你自己系统的运行状态,看有没有可疑的程序在运行. 1.2是分析一个恶意软件,就分析Exp2或Exp3中生成后门软件:分析工具 ...

  2. 926. Flip String to Monotone Increasing

    A string of '0's and '1's is monotone increasing if it consists of some number of '0's (possibly 0), ...

  3. PHP 5.6 如何使用 CURL 上传文件

    以前我们通过 PHP 的 cURL 上传文件是,是使用“@+文件全路径”的来实现的: curl_setopt(ch, CURLOPT_POSTFIELDS, array( 'file' => ' ...

  4. nodejs改变代码不需要重启的方法

    1.node 搭建本地服务器 在F:/node文件夹下新建app.js const http = require('http'); http.createServer((req, res) => ...

  5. collectd+influxdb+grafana

    今天一天都在弄这个,最终发现在配置grafana的时候选择influxdb的版本时候选错了.(挠头~~~!!!) collectd的配置还算简单,基本看过配置文件就比较清楚. influxdb(Go ...

  6. Ubuntu 连接手机 不识别设备 -- 解决办法

    1.usb线连接手机,输入命令 $ lsusb Bus 004 Device 002: ID 8087:8000 Intel Corp. Bus 004 Device 001: ID 1d6b:000 ...

  7. 剑指offer四十七之求1+2+3+...+n

    一.题目 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). 二.思路 1.需利用逻辑与的短路特性实现递归终 ...

  8. Netty核心概念(4)之Bootstrap

    1.前言 第三节介绍了Netty的一些基本概念,此节介绍Netty的第一个概念Bootstrap——启动类.Netty中服务端和客户端的启动类是不一样的,这个不要搞错了,类都在bootstrap包下. ...

  9. asp.net页面传值方法汇总

    1. Get(即使用QueryString显式传递)     方式:在url后面跟参数.     特点:简单.方便.     缺点:字符串长度最长为255个字符:数据泄漏在url中.     适用数据 ...

  10. 关于开发板用tftp下载失败分析

    一.想实现开发板和PC ping通:(1)windows和linux桥接(2)用路由器将开发板和PC连接起来(3)将windows和linux以及开发板的IP设置成同一网段,注意不要和你同一个局域网的 ...