【HDU】3085&【YBT高效进阶】1基础算法/5广度优先搜索/6逃离噩梦

【HDU】3085&【YBT高效进阶】1基础算法/5广度优先搜索/6逃离噩梦

Time Limit: 2000/1000 MS (Java/Others) 、
Memory Limit: 32768/32768 K (Java/Others)

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

翻译

逃离噩梦

时间限制:2000/1000 MS(Java /其他)
内存限制:32768/32768 K(Java /其他)

问题描述

昨晚,小二月娥做了个可怕的噩梦。他梦见自己和他的女朋友分别被困在一个大迷宫中。更可怕的是,迷宫中有两个幽灵。他们将杀死人民。现在,小erriyue想知道他是否可以在鬼魂找到女朋友之前找到他的女朋友。
您可能会认为小erriyue和他的女朋友可以朝4个方向移动。每秒钟,小erriyue可以移动3步,而他的女朋友可以移动1步。幽灵是邪恶的,它们每秒都会分成几部分,占据它们两步之内的网格,直到占据整个迷宫为止。您可以假设,幽灵每隔一秒钟就会分裂,然后小erriyue和他的女朋友开始移动,如果小erriyue或他的女朋友带着鬼来到栅栏,它们就会死去。
注意:新的鬼魂也可以作为原始的鬼魂。

输入

输入以整数T开头,表示测试用例的数量。
每个测试用例均以一行包含两个整数n和m开头,表示迷宫的大小。(1 <n,m <800)
接下来的n行描述了迷宫。每行包含m个字符。字符可以是:
“。” 表示一个空的地方,所有人都可以继续行走。
“ X”表示一堵墙,只有人不能行走。
'M’代表一点点差。'G
'代表女朋友。
“ Z”表示鬼影。
保证将恰好包含一个字母M,一个字母G和两个字母Z。

输出

在一行中输出一个整数S,表示erriyue和他的女友如果可以成功见面,将在最短的时间S见面;或者输出-1表示他们不满足。

思路

BFS搜索
判断是否已经到达,boy标记为1,girl标记为2
每一轮,boy做3次,girl做1次,同时搜索;

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue> 
#include<cmath>
using namespace std;
char a[810][810];
int vis[810][810];
int n,m,way[4][2]={{0,1},{1,0},{-1,0},{0,-1}};
queue<pair<int,int> >queb,queg,empt;
pair<int,int> b,g,z1,z2;
inline int read() {
	int x = 0, f = 1; char s = getchar();
	while (s < '0' || s > '9') { if (s == '-') f = -f; s = getchar(); }
	while (s >= '0' && s <= '9') { x = x * 10 + s - '0'; s = getchar(); }
	return x * f;
}
void input()
{
	int i,j;
	cin>>n>>m;
	for(i=1;i<=n;i++)
		for(j=1;j<=m;j++)
		{
			cin>>a[i][j];
			if(a[i][j]=='M')queb.push(make_pair(i,j)),vis[i][j]=1;
			if(a[i][j]=='G')queg.push(make_pair(i,j)),vis[i][j]=2;
			if(a[i][j]=='Z')
			{
				if(z1.first==0)z1=make_pair(i,j);
				else z2=make_pair(i,j);
			}
		}
	return;
}
bool check(pair<int,int> tem,int ans)
{
	return ((abs(tem.first-z1.first)+abs(tem.second-z1.second))>(ans<<1))&&((abs(tem.first-z2.first)+abs(tem.second-z2.second))>(ans<<1));
}
void BFS()
{
	int i,j,k,ans,dx,dy;
	pair<int,int> tem,temp;
	for(ans=1;queb.size()||queg.size();ans++)
	{
		for(j=0;j<3;j++)
			for(k=queb.size();k--;)
			{
				tem=queb.front();queb.pop();
				if(check(tem,ans))
					for(i=0;i<4;i++)
					{
						temp=make_pair(tem.first+way[i][0],tem.second+way[i][1]);
						if(check(temp,ans)&&1<=temp.first&&temp.first<=n&&1<=temp.second&&temp.second<=m&&a[temp.first][temp.second]!='X'&&vis[temp.first][temp.second]!=1)
						{
							if(vis[temp.first][temp.second]==2){printf("%d\n",ans);return;}
							vis[temp.first][temp.second]=1;
							queb.push(temp);
						}
					}
			}
		for(k=queg.size();k--;)
		{
			tem=queg.front();queg.pop();
			if(check(tem,ans))
				for(i=0;i<4;i++)
				{
					temp=make_pair(tem.first+way[i][0],tem.second+way[i][1]);
					if(check(temp,ans)&&1<=temp.first&&temp.first<=n&&1<=temp.second&&temp.second<=m&&a[temp.first][temp.second]!='X'&&vis[temp.first][temp.second]!=2)
					{
						if(vis[temp.first][temp.second]==1){printf("%d\n",ans);return;}
						vis[temp.first][temp.second]=2;
						queg.push(temp);
					}
				}
		}
	}
	putchar('-'),putchar('1'),putchar('\n');
	return;
}
int main()
{
	int T,i,j;
	for(T=read();T--;)
	{
		memset(vis,0,sizeof(vis));
		queb=queg=empt;
		z1=make_pair(0,0);
		input();
		BFS();
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值