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步,鬼可以两步且可以通过墙.问男孩女孩是否可以在鬼抓住他们之前会合? 注意:每秒开始鬼先移动,然后两人开始移动. 思路:以男孩和 ...
随机推荐
- Delphi - 让Delphi10.2在Windows下开发的图形界面程序运行在64位Linux中!
FmxLinux官网:https://fmxlinux.com/ 参考: http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Linux_Applica ...
- 638. Shopping Offers
In LeetCode Store, there are some kinds of items to sell. Each item has a price. However, there are ...
- Django安装(第一个项目)
day60 从系统中选择已存在的解释器. 新建Django项目 命令行创建: django-admin startproject mysite ...
- Retrofit源码解析(上)
简介Retrofit是Square公司开发的一款针对Android网络请求的框架,官网地址http://square.github.io/retrofit/ ,在官网上有这样的一句话介绍retrofi ...
- 通过sessionStorage来根据屏幕宽度变化来加载不同的html页面
因为项目需要,分别写了移动端和PC端的两个html页面,现在需要根据不同的屏幕宽度来加载对应的页面. 先说一下本人的思路-- 刚开始我直接在加载页面的时候判断屏幕宽度,然后加载相应的页面,大家是不是也 ...
- vue 组件之间的数据传递
父组件传数据给子组件 创建数据 获取json数据 子组件传数据给父组件 1. 子组件使用$emit监听数据 getAreaValues(){ this.$emit('getAreaValues', { ...
- MFC 剪切板的使用、线程介绍
一.MFC 剪切板 CListBox *pList = (CListBox*)GetDlgItem(IDC_LIST1); // 获取ListBox控件句柄 CString strTmp; pList ...
- 【poj3252】 Round Numbers (数位DP+记忆化DFS)
题目大意:给你一个区间$[l,r]$,求在该区间内有多少整数在二进制下$0$的数量$≥1$的数量.数据范围$1≤l,r≤2*10^{9}$. 第一次用记忆化dfs写数位dp,感觉神清气爽~(原谅我这个 ...
- J04-Java IO流总结四 《 FileReader和FileWriter 》
FileReader和FileWriter的源码相对简单,下面通过分析它们的源码以更好地进行理解这两个流 1. FileReader FileReader实现了读取底层的字节数据并将其转换为字符数据的 ...
- http正向代理与反向代理
转自:https://baijiahao.baidu.com/s?id=1566988836622068&wfr=spider&for=pc 一句话总结正向代理与反向代理的区别:正向代 ...