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 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
#include
#include
#include
#include
#include
#include
#include
#include
#include
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是女朋友走过的地方
queueq[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;
}

 

你可能感兴趣的:(BFS)