hdu 3085
Nightmare Ⅱ
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 720 Accepted Submission(s): 136
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.
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.
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
#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的更多相关文章
- HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ)
HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- HDU - 3085 Nightmare Ⅱ
HDU - 3085 Nightmare Ⅱ 双向BFS,建立两个队列,让男孩女孩一起走 鬼的位置用曼哈顿距离判断一下,如果该位置与鬼的曼哈顿距离小于等于当前轮数的两倍,则已经被鬼覆盖 #includ ...
- HDU 3085 Nightmare II 双向bfs 难度:2
http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...
- HDU 3085 Nightmare Ⅱ(双向BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 题目大意:给你一张n*m地图上,上面有有 ‘. ’:路 ‘X':墙 ’Z':鬼,每秒移动2步,可 ...
- hdu 3085(双向bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 思路:双向广搜,每次从M出发,搜三步,从G出发,搜一步,然后就是判断是否走到对方已经走过的格子, ...
- 【HDU 3085】 Nightmare Ⅱ
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3085 [算法] 双向BFS [代码] #include<bits/stdc++.h> ...
- HDU 3085 Nightmare Ⅱ (双向BFS)
Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 3085 Nightmare Ⅱ 双向BFS
题意:很好理解,然后注意几点,男的可以一秒走三步,也就是三步以内的都可以,鬼可以穿墙,但是人不可以,鬼是一次走两步 分析:我刚开始男女,鬼BFS三遍,然后最后处理答案,严重超时,然后上网看题解,发现是 ...
- HDU - 3085 双向BFS + 技巧处理 [kuangbin带你飞]专题二
题意:有两只鬼,一个男孩女孩被困在迷宫中,男孩每秒可以走三步,女孩只能1步,鬼可以两步且可以通过墙.问男孩女孩是否可以在鬼抓住他们之前会合? 注意:每秒开始鬼先移动,然后两人开始移动. 思路:以男孩和 ...
随机推荐
- 五,mysql优化——sql语句优化小技巧
1,大批量插入数据 (1)对于MyISAM: alter table table_name disable keys; loading data; alter table table_name ena ...
- 如何优雅的调戏XSS
作者:i春秋作家——万年死宅 前言 这篇paper,我们将学习如何优雅的调戏XSS.我们会教大家一些不常用的,但很实用的XSS姿势.我们在正式进入主题之前,先来说一下,该篇paper将涉及的内容: 正 ...
- 对Spring 及SpringMVC的理解
spring是一个轻型容器(light-weight Container),其核心是Bean工厂(Bean Factory),用以构造我们所需要的M(Model).在此基础之上,Spring提供了AO ...
- JAVA线程本地变量ThreadLocal和私有变量的区别
ThreadLocal并不是一个Thread,而是Thread的局部变量,也许把它命名为ThreadLocalVariable更容易让人理解一些. 所以,在Java中编写线程局部变量的代码相对来说要笨 ...
- POJ 1154
#include<iostream> #include<stdio.h> #define MAXN 20 using namespace std; int DFS(int i, ...
- Lingo 做线性规划 - Marketing Applications
Reference: <An Introduction to Management Science Quantitative Approaches to Decision Making, Rev ...
- MySql登陆密码忘记-解决方案
方法一:MySQL提供跳过访问控制的命令行参数,通过在命令行以此命令启动MySQL服务器: safe_mysqld --skip-grant-tables& 即可跳过MySQL的访问控制,任何 ...
- Java学习之路(十二):IO流<二>
字符流 字符流是可以直接读写字符的IO流 使用字符流从文件中读取字符的时候,需要先读取到字节数据,让后在转换为字符 使用字符流向文件中写入字符时,需要把字符转为字节在写入文件 Reader和Write ...
- 48位MAC转化为唯一的128位IPV6地址
根据EUI_64规范,一个MAC地址生成唯一的一个IPV6地址. ①.反转MAC的第七位为1. ②.在24bit后加入FFFE. ③.在最前面加上FE80::. 示例:
- IE中的userData
之前做项目时用到了localstorage,但是考虑到浏览器存在IE8以下不兼容问题,所以来介绍以下IE中的userData. 本地存储解决方案很多,比如Flash SharedObject.Goog ...