HDU 3085 Nightmare Ⅱ

参考大佬博客有所感悟:http://blog.csdn.net/yhyyxt/article/details/50603722

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的字符矩阵,矩阵中的M速度是3格/m,G的速度是1格/m,Z是鬼,初始有两个,每秒可以变出很多的分身Z(变出的分身在下一秒钟仍然可以变出无数个分身),占领跟Z距离是2的方格,直到占领所有的方格,每次都是鬼先占领方格,然后是M跟G走,M跟G可以同时都走,也可以有一个在原地不动,一个在走。
先让M走,再让G走,每次走之前要先预判鬼的位置,因为鬼会提前走,不用太麻烦,只需要用曼哈顿距离判断一下就行了,然后是双向bfs,题并不好,因为不符合常规的搜索,这个题M只需要到达G曾经经过的位置就算追上了,很坑,我一开始死活不知道怎么写!具体请看代码:
#include <queue>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
int step;
int n, m;
char Map[808][808];
struct node{
	int x, y;
}Z[2],M,G;
int d[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
bool MAP(int x,int y) {//判断越界 
	return x>=1 && y>=1 && x<=n && y<=m;
}
bool isghost(int x, int y) { //判断是否有鬼 
	if(MAP(x,y) == 0) return 0;
	for(int i = 0; i < 2; i++) {
		if(abs(x-Z[i].x) + abs(y-Z[i].y) <= 2*step) return 0;
		if(Map[x][y] == 'X' || Map[x][y] == '\0') return 0;
	}
	return 1;
}

queue<node> q[2];
queue<node> qt;
 // 0代表M  1代表G         c1   找  c2
int bfs(int v, int p, char c1, char c2) {
	qt = q[v];
	node pre, nex;
	for(int i = 0; i < p; i++) {
		while(!qt.empty()) {
			nex = qt.front();
			q[v].pop();	
			qt.pop();
			if(isghost(nex.x, nex.y) == 0) continue;
			for(int i = 0; i < 4; i++) {
				int dx = nex.x + d[i][0];
				int dy = nex.y + d[i][1];
				if(isghost(dx, dy) == 0 || Map[dx][dy] == c1) continue;
				if(Map[dx][dy] == c2) return 1;//找到前面那个人的轨迹就行了 
				Map[dx][dy] = c1;
				pre.x = dx;pre.y = dy;
				q[v].push(pre);
			}
		}
		qt = q[v];
	}
	return 0;
}

int main() {
	int N;
	scanf("%d",&N);
	while(N--) {
		step = 0;
		memset(Z, -1, sizeof(Z));
		memset(Map, 'X', sizeof(Map));
		scanf("%d%d",&n,&m);
		for(int i = 1; i <= n; i++) {
			scanf("%s",Map[i]+1);
			for(int j = 1; j <= m; j++) {
				if(Map[i][j] == 'M') {
					M.x = i; M.y = j;
				}
				else if(Map[i][j] == 'G') {
					G.x = i; G.y = j;
				}
				else if(Map[i][j] == 'Z') {
					if(Z[0].x == -1) {
						Z[0].x = i; Z[0].y = j;
					}
					else {
						Z[1].x = i; Z[1].y = j;
					}
				}
			}
		}
		int k = 1;
		while(!q[0].empty()) q[0].pop();	
		while(!q[1].empty()) q[1].pop();
		while(!qt.empty())  qt.pop();
		q[0].push(M);    q[1].push(G);
		while(!q[0].empty() && !q[1].empty()) {
			step++;
			int k1 = bfs(0, 3, 'M', 'G'); //M 找 G
			int k2 = bfs(1, 1, 'G', 'M'); //G 找 M
			if(k1 || k2) {
				k = 0;
				break;
			}
		}
		if(k) step = -1;
		printf("%d\n",step);
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值