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.
InputThe input starts with an integer T, means the number of test cases.
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.
OutputOutput 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..XSample Output
1 1 -1
解题思路:
双向bfs,男孩3步,女孩1步,然后用哈密顿长度计算其是否可以被鬼吃掉。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(int i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define MOD 1000000007
#define cinq ios::sync_with_stdio(false)
#define pb push_back
#define mp make_pair
//#define fi first
#define se second
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=5e2+10;
using namespace std;
typedef vector<int> vi;
typedef long long ll;
typedef unsigned long long ull;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;
while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')
fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,
rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
const int N = 1e7+5;
typedef struct node{
int x;
int y;
int step;
}NODE;
void print();
int n,m;
vi mpp[805];
string s;
vector<NODE> gh;
int T;
int ct =0;
int boy_next() ;
int gir_next() ;
queue<NODE> boy,gir;
void initt()
{
while(!boy.empty()) boy.pop();
while(!gir.empty()) gir.pop();
gh.clear();
for(int i=0;i<805;i++)
mpp[i].clear();
}
int main()
{
cinq;
cin >> T;
while(T--)
{
//初始化
initt();
cin >> n>>m;
for(int i=0;i<n;i++)
{
cin >>s;
for(int j=0;j<m;j++)
{
mpp[i].push_back(0);
if(s[j]=='X')
{
mpp[i][j]=1;
}
else if(s[j]=='Z')
{
NODE t;
t.x = i;
t.y = j;
t.step=0;
gh.push_back(t);
}
else if(s[j]=='M')
{
NODE t;
t.x = i;
t.y = j;
t.step=0;
mpp[i][j]=2;
boy.push(t);
}
else if(s[j]=='G')
{
NODE t;
t.x = i;
t.y = j;
t.step=0;
mpp[i][j]=3;
gir.push(t);
}
}
}
//计数时间
ct =0;
while(1)
{
ct++;
// boy走的距离
int tt =boy_next();
if(tt==2)
{
cout <<ct<<endl;
break;
}
else if(tt==-1)
{
cout <<-1<<endl;
break;
}
tt =boy_next();
// cout << tt<<endl;
// print();
if(tt==2)
{
cout <<ct<<endl;
break;
}
else if(tt==-1)
{
cout <<-1<<endl;
break;
}
tt =boy_next();
if(tt==2)
{
cout <<ct<<endl;
break;
}
else if(tt==-1)
{
cout <<-1<<endl;
break;
}
// gir走的距离
tt =gir_next();
if(tt==2)
{
cout <<ct<<endl;
break;
}
else if(tt==-1)
{
cout <<-1<<endl;
break;
}
}
}
return 0;
}
bool check(int xx,int yy)
{
return (xx<0||yy<0||xx>=n||yy>=m
// 利用哈密顿长度计算是否遇到鬼
|| abs(xx-gh[0].x)+abs(yy-gh[0].y)<=2*ct
|| abs(xx-gh[1].x)+abs(yy-gh[1].y)<=2*ct);
}
int boy_next()
{
NODE cur,t;
if(boy.empty()) return -1;
int size = boy.size();
while(size--)
{
cur = boy.front();
boy.pop();
// 判断是否被鬼吃掉
if(check(cur.x,cur.y))
continue;
for(int i=0;i<4;i++)
{
int xx = cur.x + dir[i][0];
int yy = cur.y + dir[i][1];
if(check(xx,yy))
continue;
//判断是否相遇
if(mpp[xx][yy]==3)
{
return 2;
}
if(mpp[xx][yy]==0)
{
t.x= xx;
t.y= yy;
t.step = cur.step+1;
boy.push(t);
mpp[xx][yy]=2;
}
}
}
if(boy.empty()) return -1;
else return 1;
}
int gir_next()
{
NODE cur,t;
if(gir.empty()) return -1;
int size = gir.size();
while(size--)
{
cur = gir.front();
gir.pop();
//判断一下是否被鬼吃掉
if(check(cur.x,cur.y))
continue;
for(int i=0;i<4;i++)
{
int xx = cur.x + dir[i][0];
int yy = cur.y + dir[i][1];
if(check(xx,yy))
continue;
//判断是否相遇
if(mpp[xx][yy]==2)
{
// cout <<xx<<yy<<endl;
return 2;
}
if(mpp[xx][yy]==0)
{
t.x= xx;
t.y= yy;
t.step = cur.step+1;
gir.push(t);
mpp[xx][yy]=3;
}
}
}
if(gir.empty()) return -1;
else return 1;
}
void print()
{
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cout << mpp[i][j]<<" ";
}
cout <<endl;
}
cout<<"*****************"<<endl;
}