HDU_3085 Nightmare Ⅱ(噩梦)

小夜曲在梦中和他的女朋友被困在一个大迷宫中,两人分别与两个幽灵周旋。他们需要在幽灵找到他们之前相遇。小夜曲每秒可以移动3步,他的女朋友每秒移动1步,而幽灵会不断分裂占领周围2格。题目要求找出他们在最短时间内的相遇可能性。输入包含迷宫大小和布局,保证各角色唯一。输出为最小相遇时间或失败的-1。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

噩梦

链接

HDU_3085 Nightmare Ⅱ(噩梦)

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 T T, means the number of test cases.
Each test case starts with a line contains two integers n n n and m m m, means the size of the maze. ( 1 < n , m < 800 ) (1<n, m<800) (1<n,m<800)
The next n lines describe the maze. Each line contains m characters. The characters may be:
‘.’ \texttt ‘ \texttt . \texttt ’ . denotes an empty place, all can walk on.
‘X’ \texttt ‘ \texttt X \texttt ’ X denotes a wall, only people can’t walk on.
‘M’ \texttt ‘ \texttt M \texttt ’ M denotes little erriyue
‘G’ \texttt ‘ \texttt G \texttt ’ G denotes the girl friend.
‘Z’ \texttt ‘ \texttt Z \texttt ’ Z denotes the ghosts.
It is guaranteed that will contain exactly one letter M \texttt M M, one letter G \texttt G G and two letters Z \texttt Z Z.

Output

Output a single integer S S S in one line, denotes erriyue and his girlfriend will meet in the minimum time S S S if they can meet successfully, or output − 1 -1 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

解析

本题需要用到双向的广度优先搜索。

双向广搜的意思是在两个不同的点开始搜索,当搜索树上的两个点重合时,就找到了答案。在本题中的运用就是,从 G G G M M M 的位置开始分别进行广度优先搜索,当两个搜索都搜索到同一个位置时,所花费的代价就是答案。

还有一点要注意,判断鬼是否来到某一点时,要将两个鬼从起点到该点的曼哈顿距离与走到该点的时间进行比较。

代码

#include<algorithm>
#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
struct str
{
	int x,y;
}z[2],b,g;
int t,n,m,cnt;
int drt[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
char mapp[801][801];
bool vis[2][801][801];
queue<str> que[2];
bool check(str t)
{
	bool flag=true;
	if(abs(t.x-z[0].x)+abs(t.y-z[0].y)<=2*cnt)//判断鬼是否已经到达某一点
		flag=false;
	else if(abs(t.x-z[1].x)+abs(t.y-z[1].y)<=2*cnt)
		flag=false;
	if(t.x<0||t.y<0||t.x>=n||t.y>=m)//判断即将要走的点是否在界外 
		flag=false;
	else if(mapp[t.x][t.y]=='X')
		flag=false;
	return flag; 
}
bool bfs(int x)
{
	int tot=que[x].size();//每次连续弹出、处理的点都是同一步的点
	str now,nxt;
	while(tot--)
	{
		now=que[x].front();
		que[x].pop();
		if(!check(now))//判断鬼是否已经到达now所在的点
			continue;
		for(int i=0;i<4;i++)
		{
			nxt.x=now.x+drt[i][0];
			nxt.y=now.y+drt[i][1];
			if(check(nxt)&&!vis[x][nxt.x][nxt.y])//判断nxt所在的点是否出界、是否已经过
			{
				vis[x][nxt.x][nxt.y]=true;
				if(vis[x][nxt.x][nxt.y]&&vis[1-x][nxt.x][nxt.y])//判断nxt所在的点是否被G和M都走过
					return true;
				que[x].push(nxt);
			}
		}
	}
	return false;
}
int fun()
{
	while(!que[0].empty())//每次处理一组新的数据前先将队列清空
		que[0].pop();
	while(!que[1].empty())
		que[1].pop();
	memset(vis,0,sizeof(vis));
	que[0].push(b);
	que[1].push(g);
	vis[0][b.x][b.y]=vis[1][g.x][g.y]=true;
	while(!que[0].empty()||!que[1].empty())
	{
		cnt++;//统计时间
		for(int i=0;i<3;i++)//M每个单位时间走三步
			if(bfs(0))
				return cnt;
		if(bfs(1))//G每个单位时间走一步
			return cnt;
	}
	return -1;
}
int main()
{
	scanf("%d",&t);
	while(t--)
	{
		int zx=0;
		cnt=0;
		scanf("%d%d",&n,&m);
		for(int i=0;i<n;i++)
		{
			scanf("%s",&mapp[i]);//一定要用scanf输入字符串,用cin会超时,输入单个字符会出错
			for(int j=0;j<m;j++)
			{
				if(mapp[i][j]=='Z')
				{
					z[zx].x=i,z[zx].y=j;
					zx++;
				}
				if(mapp[i][j]=='M')
					b.x=i,b.y=j;
				if(mapp[i][j]=='G')
					g.x=i,g.y=j;
			}
		}
		printf("%d\n",fun());
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值