1. 程式人生 > >Nightmare Ⅱ HDU

Nightmare Ⅱ HDU

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;
}