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
1 1 -1
题意:有M,G两人和鬼魂(Z)在n*m的方格内,M每秒走3步,G每秒走一步,鬼魂每秒走2步,问是否能
不遇到鬼魂下两人相遇,鬼魂可以穿墙(X),人不可以。初始鬼魂有2个。
注意:每秒开始时鬼先动
-
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
int dir[4][2]={{-1,0},{0,1},{0,-1},{1,0}};
int n,m,step;
char map[808][808];
struct node
{
int x;
int y;
node(int x=-1,int y=-1)
{
this->x=x;
this->y=y;
}
}G,M,Z[2];
queue<node>q[3];
bool ok(int x,int y)
{
for (int i=0; i<2; i++)
{
if(abs(x-Z[i].x)+abs(y-Z[i].y)<=2*step)
return false;
}
return true;
}
void clear()
{
for (int i=0; i<3; i++)
{
while (!q[i].empty())
q[i].pop();
}
}
bool check(int x,int y)
{
if(x<0 || x>=n || y<0 || y>=m ||map[x][y]=='X')
return false;
return true;
}
bool bfs(int id,int num,char sour,char flag) //id代表队列编号,num:移动步数
{
node temp1,temp2;
q[2]=q[id];
for (int i=0; i<num; i++)
{
while (!q[2].empty())//对队列中的每个点进行处理
{
temp1=q[2].front();
q[2].pop();
q[id].pop();
if (ok(temp1.x, temp1.y))//每次开始鬼先动,这里需要判断
{
for (int j=0; j<4; j++)
{
temp2=temp1;
temp2.x+=dir[j][0];
temp2.y+=dir[j][1];
if(check(temp2.x, temp2.y) &&ok(temp2.x, temp2.y) && map[temp2.x][temp2.y]!=sour)
{
if(map[temp2.x][temp2.y]==flag)
return true;
map[temp2.x][temp2.y]=sour;
q[id].push(temp2);
}
}
}
}
q[2]=q[id];
}
return false;
}
int solve()
{
step=0;
clear();
q[0].push(M);//m队列
q[1].push(G);//g队列
while (!q[0].empty() && !q[1].empty())
{
step++;
if(bfs(0,3,'M','G')||bfs(1,1,'G','M'))
return step;
}
return -1;
}
int main()
{
int t;
scanf("%d",&t);
int cnt;
while (t--)
{
cnt=0;
scanf("%d%d",&n,&m);
getchar();
for (int i=0; i<n; i++)
{
scanf("%s",map[i]);
for (int j=0; j<m; j++)
{
if(map[i][j]=='M')
{
M.x=i;
M.y=j;
}
else if(map[i][j]=='G')
{
G.x=i;
G.y=j;
}
else if(map[i][j]=='Z')
{
Z[cnt].x=i;
Z[cnt++].y=j;
}
}
getchar();
}
printf("%d\n",solve());
}
return 0;
}