Problem 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, means the number of test cases.
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.
Output
Output 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..X
Sample Output
1
1
-1
博客参考:https://blog.youkuaiyun.com/mengxiang000000/article/details/50298967
分析一下,这里鬼题目限定只有两个,那么只需要判断每一秒搜到的点到鬼的曼哈顿距离就行了!不需要真正的把鬼扩散;
代码
#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
int t;
int n,m;
char a[805][805];
int vis2[805][805];
int vis3[805][805];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,-1,1};
int xg,x2;
int yg,y2;
struct node
{
int x;
int y;
}tt[2];
int st;
int judge(int i,int j)
{
if(i<0||i>=n||j<0||j>=m) return 0;
if(a[i][j]=='X') return 0;
for(int k=0;k<2;k++) //曼哈顿距离的应用,相当于鬼不动,人在走
if((abs(i-tt[k].x)+abs(j-tt[k].y))<=2*st) return 0;
return 1;
}
int bfs()
{
queue<node> q2;//g
queue<node> q3;//b
node h;
h.x=xg;
h.y=yg;
vis2[xg][yg]=1;//g
vis3[x2][y2]=1;//b
q2.push(h);//g
h.x=x2;
h.y=y2;
q3.push(h);//b
while(!q2.empty()||!q3.empty()) //双向bfs
{
st++;
int s2=q2.size();
while(s2--) //girl go
{
node f1=q2.front();
q2.pop();
if(judge(f1.x,f1.y)==0) continue;
for(int i=0;i<4;++i)
{
int xx=f1.x+dx[i];
int yy=f1.y+dy[i];
if(judge(xx,yy)==0) continue;
else
{
if(vis3[xx][yy]==1) return st;
if(!vis2[xx][yy])
{
vis2[xx][yy]=1;
node hh;
hh.x=xx;
hh.y=yy;
q2.push(hh);
}
}
}
}
int k=3;
while(k--)//boy go
{
int s3=q3.size();
while(s3--)
{
node f1=q3.front();
q3.pop();
if(judge(f1.x,f1.y)==0) continue;
for(int i=0;i<4;++i)
{
int xx=f1.x+dx[i];
int yy=f1.y+dy[i];
if(judge(xx,yy)==0) continue;
if(vis2[xx][yy]) return st;
if(!vis3[xx][yy])
{
node hh;
vis3[xx][yy]=1;
hh.x=xx;
hh.y=yy;
q3.push(hh);
}
}
}
}
}
return -1;
}
int main()
{
scanf("%d",&t);
while(t--)
{
st=0;
int k=0;
memset(vis2,0,sizeof vis2);
memset(vis3,0,sizeof vis3);
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
scanf("%s",a[i]);
for(int i=0;i<n;++i)
for(int j=0;j<m;++j)
if(a[i][j]=='G')
{
xg=i;
yg=j;
}
else if(a[i][j]=='M')
{
x2=i;
y2=j;
}
else if(a[i][j]=='Z')
{
tt[k].x=i;
tt[k].y=j;
k++;
}
cout<< bfs()<<endl;
}
return 0;
}
本文介绍了一个迷宫逃脱问题,主人公需要在两个恶鬼占据整个迷宫之前找到他的女友。使用双向 BFS 算法,并结合曼哈顿距离来判断每一步的安全性,实现了在最短时间内完成目标的算法。
2658

被折叠的 条评论
为什么被折叠?



