HDU 3085 Nightmare Ⅱ (双向广搜)

转载自:http://blog.csdn.net/u012861385/article/details/32913731

Nightmare Ⅱ

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


题意两个鬼 向四周扩散 墙也可以 所到之地都变鬼   每秒两步 

男人一秒走三步  女人一 步

问男女 能   在鬼抓 到之前相遇吗(一个被抓就GG) 

如果能 就输出秒数

思路:对男女分别进行bfs  也相当于扩散,所到之地变为他们的地盘  当他们的地盘遇到时就是他们相遇了

在对男的bfs时  因为他要走三步 所以需要一个队列作为存储每部之后的状态。

如何对鬼进行判断?即人到鬼的距离小于等于两倍的时间的时候,他将会被鬼遇到而死亡。

#include<stdio.h>
#include<string.h>
#include<string>
#include<queue>
#include<map>
#include<iostream>
#include<algorithm>
#define maxn 800
using namespace std;

struct node //人的坐标
{
	int x,y;
	node (){}
	node (int xx,int yy)
	{
		x=xx;
		y=yy;
	}
}gg,mm,zz[2];//女 男 两鬼
int step;//已经过得时间
char maps[maxn][maxn];
int n,m;
int go[4][2]={0,1,0,-1,1,0,-1,0};
queue<node>q[3];//男的  女的  还有一个用来过程中的保存
void clear()
{
	for(int i=0;i<3;i++)
		while(!q[i].empty())q[i].pop();
}
bool dis(int x ,int y)
{
	if(x>=0&&x<n&&y>=0&&y<m&&maps[x][y]!='X')
	{
		for(int i=0;i<2;i++)
		{
			int ans=abs(x-zz[i].x)+abs(y-zz[i].y);
			if(ans<=2*step)return false;
		}
		return true;
	}
	return false;
}
bool bfs(int num,int v,char s,char e)//num:0为mm或是1是gg,v每秒走的步数  s是起点  e是目的地
{
	q[2]=q[num];//另外开个队列保存
	for(int i=0;i<v;i++)
	{
		node u,v;
		while(!q[2].empty())
		{
			u=q[2].front();
			q[2].pop();
			q[num].pop();
			if(!dis(u.x,u.y))//判断与鬼的距离 判断是否在范围内  判断是否走到墙上
				continue;
			for(int j=0;j<4;j++)
			{
				v=u;
				v.x+=go[j][0];
				v.y+=go[j][1];
				if(!dis(v.x,v.y)||maps[v.x][v.y]==s)continue;
				if(maps[v.x][v.y]==e)return true;
				maps[v.x][v.y]=s;
				q[num].push(v);
			}

		}
		q[2]=q[num];//把走了第i步的状态保存到q[2],以免重复
	}
	return false;
}
void solve()
{
	step=0;
	clear();
	q[0].push(mm);
	q[1].push(gg);
	while(!q[0].empty()&&!q[1].empty())//两个都不为  为空意味着无路可走
	{
		step++;
		if(bfs(0,3,'M','G')||bfs(1,1,'G','M'))
		{
			printf("%d\n",step);
			return ;
		}
	}
	printf("-1\n");
			return ;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
	{
		scanf("%d%d",&n,&m);
		int k=0;
		for(int i=0;i<n;i++)
		{
			scanf("%s",maps[i]);
			for(int j=0;j<m;j++)
			{
				if(maps[i][j]=='M')
				{
					mm=node(i,j);
				}
				else if(maps[i][j]=='G')
				{
					gg=node(i,j);
				}
				else if(maps[i][j]=='Z')
				{
					zz[k++]=node(i,j);
				}
			}
		}
		solve();
	}
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值