HDU3085(双向BFS+曼哈顿距离)题解

Nightmare Ⅱ

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3012    Accepted Submission(s): 856


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


思路:

双向BFS,只要其中一个人走到了另一个人走过的地方就算相遇。

判断鬼这里用到了曼哈顿距离,就是鬼可以每步向外扩张两格范围,所以只要在每次走完看一下有没有和鬼超过2*step就行了(这里要注意鬼先走)。还有每次走之前要判断走之前的点有没有被鬼占领,一直wa在这里找不到哪里错了。

Code:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<queue>
#include<cmath>
//#include<map>
#include<iostream>
#include<algorithm>
#define INF 0x3f3f3f3f
const int N=810;
using namespace std;
struct node{
	int x,y;
};
int step,n,m,mx,my,gx,gy,zx[2],zy[2],to[4][2]={0,1,0,-1,1,0,-1,0};
char map[N][N];
int vis[2][N][N];
queue<node> q[2];
int bfs(int x){
	node a,b;
	int Count=q[x].size();
	while(Count--){
		a=q[x].front();
		q[x].pop();
		
		/**这里要注意走之前一定要先对a进行判断**/
		if((abs(a.x-zx[0])+abs(a.y-zy[0]))<=2*step) continue;	//曼哈顿距离 
		if((abs(a.x-zx[1])+abs(a.y-zy[1]))<=2*step) continue;
		
		for(int i=0;i<4;i++){
			b.x=a.x+to[i][0];
			b.y=a.y+to[i][1];
			if(b.x<0 || b.y<0 || b.x>=n || b.y>=m) continue;
			if(map[b.x][b.y]=='X') continue;
			if(vis[x][b.x][b.y]) continue;
			if((abs(b.x-zx[0])+abs(b.y-zy[0]))<=2*step) continue;	//曼哈顿距离 
			if((abs(b.x-zx[1])+abs(b.y-zy[1]))<=2*step) continue;
			vis[x][b.x][b.y]=1;
			if(vis[0][b.x][b.y] && vis[1][b.x][b.y]) return 1;
			q[x].push(b);
		}
	}
	return 0;
}
int solve(){
	node a;
	memset(vis,0,sizeof(vis));
	while(!q[0].empty()) q[0].pop();
	while(!q[1].empty()) q[1].pop();
	vis[0][mx][my]=1;
	vis[1][gx][gy]=1;
	a.x=mx;a.y=my;
	q[0].push(a);
	a.x=gx;a.y=gy;
	q[1].push(a);
	step=0;
	while((!q[0].empty()) || (!q[1].empty())){
		step++; 
		if(bfs(0)) return step;
		if(bfs(0)) return step;
		if(bfs(0)) return step;
		if(bfs(1)) return step;
	}
	return -1;
}
int main(){
	int c,k;
	scanf("%d",&c);
	while(c--){
		scanf("%d%d",&n,&m);
		k=0;
		for(int i=0;i<n;i++){
			scanf("%s",map[i]);
			for(int j=0;j<m;j++){
				if(map[i][j]=='M'){
					mx=i;my=j;
				}
				else if(map[i][j]=='G'){
					gx=i;gy=j;
				}
				else if(map[i][j]=='Z'){
					zx[k]=i;zy[k++]=j;
				}
			}
		}
		int ans=solve();
		printf("%d
",ans);
	}
	return 0;
}


原文地址:https://www.cnblogs.com/KirinSB/p/9409129.html