1. 程式人生 > >HDU 3085 Nightmare Ⅱ [雙向BFS]

HDU 3085 Nightmare Ⅱ [雙向BFS]

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.

雙向BFS

#include<cstdio>
#include<queue>
#include<cmath>
#include<cstring>
using namespace std;
int n,m,T,t;
char ch[810][810];
bool vis[810][810][2];
struct pont{
	int x,y;
}e[1000][2],gh[3],bo,gi;
queue <pont> q[2];
int dx[4]={0,0,1,-1},dy[4]={1,-1,0,0};
int dis(int x,int y,int ex,int ey){
	return abs(x-ex)+abs(y-ey);
}
bool cherk(int x,int y){
	if(x<1||x>n||y<1||y>m||ch[x][y]=='X')return false;
	return true;
}
bool cherk2(int x,int y,int ste){
	for(int i=1;i<=2;i++)
		if(dis(x,y,gh[i].x,gh[i].y)<=ste*2)return false;
	return true;
}
bool bfs(int typ,int ste){
	int siz=q[typ].size();
	while(siz--){
		pont po=q[typ].front(),to;q[typ].pop();
		if(!cherk(po.x,po.y)||!cherk2(po.x,po.y,ste))continue;
		for(int i=0;i<4;i++){
			to.x=po.x+dx[i],to.y=po.y+dy[i];
			if(!cherk(to.x,to.y)||!cherk2(to.x,to.y,ste))continue;
			if(vis[to.x][to.y][typ^1])return true;
			if(vis[to.x][to.y][typ])continue;
			vis[to.x][to.y][typ]=1;
			q[typ].push(to);
		}
	}
	return false;
}
int work(){
	memset(vis,0,sizeof(vis));
	vis[bo.x][bo.y][0]=1;vis[gi.x][gi.y][1]=1;
	while(!q[0].empty())q[0].pop();
	while(!q[1].empty())q[1].pop();
	q[0].push(bo);q[1].push(gi);
	int step=0;
	while(!q[0].empty()||!q[1].empty()){
		step++;
		for(int i=1;i<=3;i++)
		  if(bfs(0,step))return step;	
		if(bfs(1,step))return step;
	}
	return -1;
}
int main(){
	scanf("%d",&T);
	while(T--){
	  scanf("%d%d",&n,&m);
	  t=0;
	  for(int i=1;i<=n;i++){
	  	scanf("%s",ch[i]+1);
		for(int j=1;j<=m;j++){
	    	if(ch[i][j]=='M')bo.x=i,bo.y=j;
	    	if(ch[i][j]=='G')gi.x=i,gi.y=j;
	    	if(ch[i][j]=='Z')gh[++t].x=i,gh[t].y=j;
		}
	  }
		printf("%d\n",work());	
	};
	return 0;
 }