Nightmare Ⅱ 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

题意:输入n*m的字符矩阵,矩阵中的M速度是3格/m,G的速度是1格/m,Z是鬼,初始有两个,每秒可以变出很多的分身Z(变出的分身在下一秒钟仍然可以变出无数个分身),占领跟Z距离是2的方格,直到占领所有的方格,每次都是鬼先占领方格,然后是M跟G走,M跟G可以同时都走,也可以有一个在原地不动,一个在走

思路:用两个 bfs ,一个是M的,一个是G,两队列同时走,没走一步都要判断;看看这个点被鬼占领了吗和这个点被要找的那个人走过了吗,只要走过了就说明在鬼没有扩展到这时,他们两个可以回合 (因为他们两个可以选择原地不动),看鬼是否占领了吗,因为鬼可以穿墙,所以可以用曼哈顿距离判断这个点是否被占领;

学一下,两个队列一起走 和 走一步判断一下;

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define Max 880
#include<queue>
#include<stdlib.h>
bool book[2][Max][Max];
char str[Max][Max];
int a[4][2] = {0,-1,1,0,0,1,-1,0};
struct node
{
	int x,y;
}M,G,Z[2];
int n,m,step;
queue<node> q[2];
int check(int y,int x)     // 因为鬼是一圈一圈的扩,所以可以利用曼哈顿距离判断 
{                         //每一秒能扩展到离当前点有两单位的位置
	for(int i = 0;i<2;i++)
	{
		int tt = abs(Z[i].x-x)+abs(Z[i].y-y);
		if(tt<=2*step)
			return 0;
	}		
	return 1;
}

int bfs(int tt)
{
	int sum = q[tt].size();
	while(sum--)     // 一圈一圈的判断; 
	{
		node end,star = q[tt].front();
		q[tt].pop();
		if(!check(star.y,star.x)) continue;
		// 因为每一秒开始都是鬼先走,鬼先判断自己的范围内有M,G的存在吗?若存在就说明这条路行不通; 
		for(int i = 0;i<4;i++)
		{
			int x = star.x + a[i][0];
			int y = star.y + a[i][1];
			if(y<0||x<0||y>=m||x>=n||str[y][x]=='X'||book[tt][y][x])
				continue;
			if(check(y,x))   //这是这一秒内,看看有没有把这点包围;有剪枝的效果 
			{
				book[tt][y][x] = 1;
				if(book[1-tt][y][x]) return 1;
				end.y = y;
				end.x = x;
				q[tt].push(end);
			}
		}
	}
	return 0;
}

int build()       // 返回 step 或 -1; 
{
	while(!q[0].empty()) q[0].pop();
	while(!q[1].empty()) q[1].pop();
	memset(book,0,sizeof(book));
	step = 1;
	book[0][M.y][M.x] = 1;
	book[1][G.y][G.x] = 1;
	q[0].push(M);
	q[1].push(G);
	while(!q[0].empty()||!q[1].empty())
	{
		for(int i = 0;i<3;i++)
			if(bfs(0)) return step;
		if(bfs(1)) return step;
		step++;            // 一秒一秒的判断; 
	}
	return -1;
}
int main()
{
	int i,j,t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&m,&n);
		int cot = 0;
		for(i = 0;i<m;i++)
		{
			scanf("%s",str[i]);
			for(j = 0;j<n;j++)
			{
				if(str[i][j]=='Z')
				{
					Z[cot].y = i;
					Z[cot++].x = j;
				}
				if(str[i][j]=='M')
				{
					M.y = i;
					M.x = j;
				}
				if(str[i][j]=='G')
				{
					G.y = i;
					G.x = j;
				}
			}
		}
		printf("%d\n",build());
	}
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/obsorb_knowledge/article/details/81353935