噩梦
链接
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
T
T, means the number of test cases.
Each test case starts with a line contains two integers
n
n
n and
m
m
m, means the size of the maze.
(
1
<
n
,
m
<
800
)
(1<n, m<800)
(1<n,m<800)
The next n lines describe the maze. Each line contains m characters. The characters may be:
‘.’
\texttt ‘ \texttt . \texttt ’
‘.’ denotes an empty place, all can walk on.
‘X’
\texttt ‘ \texttt X \texttt ’
‘X’ denotes a wall, only people can’t walk on.
‘M’
\texttt ‘ \texttt M \texttt ’
‘M’ denotes little erriyue
‘G’
\texttt ‘ \texttt G \texttt ’
‘G’ denotes the girl friend.
‘Z’
\texttt ‘ \texttt Z \texttt ’
‘Z’ denotes the ghosts.
It is guaranteed that will contain exactly one letter
M
\texttt M
M, one letter
G
\texttt G
G and two letters
Z
\texttt Z
Z.
Output
Output a single integer S S S in one line, denotes erriyue and his girlfriend will meet in the minimum time S S S if they can meet successfully, or output − 1 -1 −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
解析
本题需要用到双向的广度优先搜索。
双向广搜的意思是在两个不同的点开始搜索,当搜索树上的两个点重合时,就找到了答案。在本题中的运用就是,从 G G G 和 M M M 的位置开始分别进行广度优先搜索,当两个搜索都搜索到同一个位置时,所花费的代价就是答案。
还有一点要注意,判断鬼是否来到某一点时,要将两个鬼从起点到该点的曼哈顿距离与走到该点的时间进行比较。
代码
#include<algorithm>
#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
struct str
{
int x,y;
}z[2],b,g;
int t,n,m,cnt;
int drt[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
char mapp[801][801];
bool vis[2][801][801];
queue<str> que[2];
bool check(str t)
{
bool flag=true;
if(abs(t.x-z[0].x)+abs(t.y-z[0].y)<=2*cnt)//判断鬼是否已经到达某一点
flag=false;
else if(abs(t.x-z[1].x)+abs(t.y-z[1].y)<=2*cnt)
flag=false;
if(t.x<0||t.y<0||t.x>=n||t.y>=m)//判断即将要走的点是否在界外
flag=false;
else if(mapp[t.x][t.y]=='X')
flag=false;
return flag;
}
bool bfs(int x)
{
int tot=que[x].size();//每次连续弹出、处理的点都是同一步的点
str now,nxt;
while(tot--)
{
now=que[x].front();
que[x].pop();
if(!check(now))//判断鬼是否已经到达now所在的点
continue;
for(int i=0;i<4;i++)
{
nxt.x=now.x+drt[i][0];
nxt.y=now.y+drt[i][1];
if(check(nxt)&&!vis[x][nxt.x][nxt.y])//判断nxt所在的点是否出界、是否已经过
{
vis[x][nxt.x][nxt.y]=true;
if(vis[x][nxt.x][nxt.y]&&vis[1-x][nxt.x][nxt.y])//判断nxt所在的点是否被G和M都走过
return true;
que[x].push(nxt);
}
}
}
return false;
}
int fun()
{
while(!que[0].empty())//每次处理一组新的数据前先将队列清空
que[0].pop();
while(!que[1].empty())
que[1].pop();
memset(vis,0,sizeof(vis));
que[0].push(b);
que[1].push(g);
vis[0][b.x][b.y]=vis[1][g.x][g.y]=true;
while(!que[0].empty()||!que[1].empty())
{
cnt++;//统计时间
for(int i=0;i<3;i++)//M每个单位时间走三步
if(bfs(0))
return cnt;
if(bfs(1))//G每个单位时间走一步
return cnt;
}
return -1;
}
int main()
{
scanf("%d",&t);
while(t--)
{
int zx=0;
cnt=0;
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
{
scanf("%s",&mapp[i]);//一定要用scanf输入字符串,用cin会超时,输入单个字符会出错
for(int j=0;j<m;j++)
{
if(mapp[i][j]=='Z')
{
z[zx].x=i,z[zx].y=j;
zx++;
}
if(mapp[i][j]=='M')
b.x=i,b.y=j;
if(mapp[i][j]=='G')
g.x=i,g.y=j;
}
}
printf("%d\n",fun());
}
return 0;
}