1. 程式人生 > >hdu3085Nightmare Ⅱ(雙向BFS)

hdu3085Nightmare Ⅱ(雙向BFS)

傳送門:http://acm.hdu.edu.cn/showproblem.php?pid=3085

Nightmare Ⅱ

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3738    Accepted Submission(s): 1073


 

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

 

題意:n*m的地圖,有一個小e和他的女朋友還有兩個鬼怪。x是牆(鬼能穿牆der),m是小e,g是女朋友,z是鬼怪。小e跑的賊快,每秒可以跑3個單位,女朋友每秒一個。每個鬼怪每秒可以將自己佔據區域擴張兩個單位距離,也就是說第k秒後所有和鬼怪的曼哈頓距離不超過2*k的位置都會被鬼怪佔領。求小e和女朋友不見鬼的情況下最短碰頭時間,見鬼出-1

 

思路:雙向BFS,小e和女朋友各一個佇列,每次擴張時如果新狀態和鬼怪的曼哈頓距離如果<=秒數*2就不合法,當出現一個位置兩個人都到達就ok

 

吐槽:忘記更新鬼怪的統計變數cnt,debug了好久quq

 

程式碼:

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<iostream>
#include<map>
#include<vector>
#include<set>
#include<queue>
using namespace std;
const int maxv=811;

struct node
{
	int x,y;
}zom[2],my,girl;

int dr[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
char mp[maxv][maxv];
int cnt,t,n,m,ti;
int vis[maxv][maxv][2];//最後一個引數0代表小e走過的地方,1是女朋友走過的地方
queue<node>q[2],now;

int check(node a)
{
	if(a.x<0||a.x>=n||a.y<0||a.y>=m)
		return 0;
	if(mp[a.x][a.y]=='X')
		return 0;
	int dist1=abs(a.x-zom[0].x)+abs(a.y-zom[0].y);
	int dist2=abs(a.x-zom[1].x)+abs(a.y-zom[1].y);
	if(dist1<=2*ti||dist2<=2*ti)
		return 0;
	return 1;
}

int bfs(int id,int number)
{
	now=q[id];
	for(int i=1;i<=number;i++)
	{
		while(!now.empty())
		{
			node xx=now.front();
			now.pop();
			q[id].pop();
			if(check(xx)==0)
				continue;
			for(int j=0;j<=3;j++)
			{
				node yy;
				yy.x=xx.x+dr[j][0];
				yy.y=xx.y+dr[j][1];
				if(check(yy)==0||vis[yy.x][yy.y][id]==1)
					continue;
				if(vis[yy.x][yy.y][1-id]==1)//如果對方到過這個地方,就可以會和啦!
					return 1;
				vis[yy.x][yy.y][id]=1;
				q[id].push(yy);
			}
		}
		now=q[id];
	}
	return 0;
}

int main()
{
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d %d",&n,&m);
		cnt=0;
		for(int i=0;i<=n-1;i++)
		{
			scanf("%s",mp[i]);
			for(int j=0;j<=m-1;j++)
			{
				if(mp[i][j]=='G')
				{
					girl.x=i;
					girl.y=j;
				}
				if(mp[i][j]=='M')
				{
					my.x=i;
					my.y=j;
				}
				if(mp[i][j]=='Z')
				{
					zom[cnt].x=i;
					zom[cnt].y=j;
					cnt++;
				}
			}
		}
		while(!q[0].empty())
			q[0].pop();
		while(!q[1].empty())
			q[1].pop();
		while(!now.empty())
			now.pop();
		q[0].push(my);
		q[1].push(girl);
		memset(vis,0,sizeof(vis));
		vis[my.x][my.y][0]=1;
		vis[girl.x][girl.y][1]=1;
		ti=0;
		int flag=0;
		while((!q[0].empty())&&(!q[1].empty()))
		{
			ti++;
			if(bfs(0,3)||bfs(1,1))
			{
				flag=1;
				break;
			}
		}
		if(flag)
			printf("%d\n",ti);
		else
			puts("-1");
	}
	return 0;
}