HDU 3085 双向 BFS

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

题意:

      erriye 梦见女友被困在迷宫里了,现在 erriye 需要去解救他的的女友

      给出他女友和他的位置

      迷宫里有两个ghost,每秒钟会分生出多个ghost占据在他2步之内的所有格子little erriye 每秒可以移动3步

      grilfriend每秒可以移动一步

      ghost、erriye、grilfriend都只能上下左右移动(且人不能穿透墙壁,鬼魂可以)

      人一旦与鬼魂占据同一格子,则被杀死

      问:little erriye 最少需要多久才能解救其女友?(如果解救失败,输出-1)

鬼是先走的,鬼走完,然后,两个人再走,erriye走3步,grilfriend走一步,这就需要3个BFS但是,鬼距离人的距离可以拢曼哈顿距离表示,

大家先来了解一下曼哈顿距离https://blog.csdn.net/CQBZLYTina/article/details/75149587如果没看懂,请百度。。。

然后就是两个人走,但是我不会同时进行双向BFS,因为我不会写,这样的话我们就先让一个人先走,然后另一个人再走。用一个3维数组来存 erriye 和他grilfriend 的位置,vis[0][805][805],用来标记其中一个人能到达的位置,vis[1][805][805],用来标记另一个人的位置,如果在鬼抓到之前,这两个图有交叉(两个人都能到达这个点),则两人相遇。

上代码

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <cmath>
using namespace std;

const int to[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; 
char map[805][805];
int vis[2][805][805];
int n,m,step;

struct node
{
    int x,y;
} bf,gf,z[2]; //分别表示初末位置,和两个小鬼的位置

queue<node> q[2];

void init()
{
    int cnt=0;
    scanf("%d%d",&n,&m);
    for(int i=0; i<n; i++)
    {
    	scanf("%s",map[i]);
        for(int j=0; j<m; j++)
        {
        
            if(map[i][j] =='M') //记录初末位置
            {
                bf.x = i;
            	bf.y = j;
            }
            else if(map[i][j] =='G')
            {
                gf.x = i;
                gf.y = j;
            }
            else if(map[i][j] =='Z')
            {
                z[cnt].x = i;
                z[cnt++].y = j; //下次是第二个小鬼
            }
        }
    }
}
int judge(node b)
{
    if(b.x<0 || b.y<0 || b.x>=n || b.y>=m || map[b.x][b.y] =='X')//判断越界,判断墙
        return 0;
    if((abs(b.x-z[0].x) + abs(b.y-z[0].y)) <= 2*step)
        return 0;
    if((abs(b.x-z[1].x) + abs(b.y-z[1].y)) <= 2*step) //用曼哈顿距离表示 是否会碰到分裂的小鬼
        return 0;
    return 1;
}
int bfs(int w){
	node now,next;
	int i,sum;
	sum=q[w].size();
	while(sum--)
	{
		now=q[w].front();
		q[w].pop();
		if(judge(now)==1)
		for(int i=0;i<4;i++)
		{
			next.x=now.x+to[i][0];
			next.y=now.y+to[i][1];
			if(judge(next)==0)
			continue;
			if(vis[w][next.x][next.y]==0)
			{
				if(vis[w^1][next.x][next.y]==1)
				return 1;
				vis[w][next.x][next.y]=1;
				q[w].push(next);
			}			
		}
	}
	return 0;
}
int solve()
{
    while(!q[0].empty())
        q[0].pop();
    while(!q[1].empty())
        q[1].pop();

    q[0].push(bf);
    q[1].push(gf);
    memset(vis,0,sizeof(vis));
    vis[0][bf.x][bf.y]=vis[1][gf.x][gf.y]=1;
    step=0;

    while ((!q[0].empty()) || (!q[1].empty()))
    {
        step++;
        if (bfs(0)==1)
            return step;
        if (bfs(0)==1)
            return step;
        if (bfs(0)==1) //走三步
            return step;
        if (bfs(1)==1) //走一步
            return step;
    }
    return -1;
}
int main()
{
    int Case;
    scanf("%d",&Case);
    while (Case--)
    {
        init();
        printf("%d\n",solve());
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Harington/article/details/82109014