hdu2216之BFS

Game III

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1112    Accepted Submission(s): 320


Problem Description
Zjt and Sara will take part in a game, named Game III. Zjt and Sara will be in a maze, and Zjt must find Sara. There are some strang rules in this maze. If Zjt move a step, Sara will move a step in opposite direction.
Now give you the map , you shold find out the minimum steps, Zjt have to move. We say Zjt meet Sara, if they are in the same position or they are adjacent . 
Zjt can only move to a empty position int four diraction (up, left, right, down). At the same time, Sara will move to a position in opposite direction, if there is empty. Otherwise , she will not move to any position.
The map is a N*M two-dimensional array. The position Zjt stays now is marked Z, and the position, where Sara stays, is marked E.

>  . : empty position
>  X: the wall
>  Z: the position Zjt now stay
>  S: the position Sara now stay

Your task is to find out the minimum steps they meet each other.
 

Input
The input contains several test cases. Each test case starts with a line contains three number N ,M (2<= N <= 20, 2 <= M <= 20 ) indicate the size of the map. Then N lines follows, each line contains M character. A Z and a S will be in the map as the discription above.
 

Output
For each test case, you should print the minimum steps. “Bad Luck!” will be print, if they can't meet each other.
 

Sample Input
   
   
   
   
4 4 XXXX .Z.. .XS. XXXX 4 4 XXXX .Z.. .X.S XXXX 4 4 XXXX .ZX. .XS. XXXX
 

Sample Output
   
   
   
   
1 1 Bad Luck!
 
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 99999999
using namespace std;

const int MAX=20+10;
char Map[MAX][MAX];
int mark[MAX][MAX][MAX][MAX];//标记在位置i,j且终点位置在x,y
int n,m;
int dir[4][2]={0,1,1,0,0,-1,-1,0};

struct Node{
	int x,y,ex,ey,time;//记录本点位置和终点位置
	Node(){}
	Node(int X,int Y,int Ex,int Ey,int Time):x(X),y(Y),ex(Ex),ey(Ey),time(Time){}
}start;

int BFS(int &flag){
	if(start.x == start.ex && start.y == start.ey)return start.time;
	if(start.x == start.ex && (start.y-start.ey == 1 || start.y-start.ey == -1))return start.time;
	if(start.y == start.ey && (start.x-start.ex == 1 || start.x-start.ex == -1))return start.time;
	queue<Node>q;
	Node oq,next;
	q.push(start);
	mark[start.x][start.y][start.ex][start.ey]=flag;
	while(!q.empty()){
		oq=q.front();
		q.pop();
		for(int i=0;i<4;++i){
			int k=(i+2)%4;
			next=Node(oq.x+dir[i][0],oq.y+dir[i][1],oq.ex+dir[k][0],oq.ey+dir[k][1],oq.time+1);
			if(next.x<0 || next.y<0 || next.x>=n || next.y>=m)continue;
			if(next.ex<0 || next.ey<0 || next.ex>=n || next.ey>=m)next.ex=oq.ex,next.ey=oq.ey;;
			if(Map[next.ex][next.ey] == 'X')next.ex=oq.ex,next.ey=oq.ey;
			if(Map[next.x][next.y] == 'X' || mark[next.x][next.y][next.ex][next.ey] == flag)continue;
			mark[next.x][next.y][next.ex][next.ey]=flag;
			if(next.x == next.ex && next.y == next.ey)return next.time;
			if(next.x == next.ex && (next.y-next.ey == 1 || next.y-next.ey == -1))return next.time;
			if(next.y == next.ey && (next.x-next.ex == 1 || next.x-next.ex == -1))return next.time;
			q.push(next);
		}
	}
	return -1;
}

int main(){
	int num=0;
	while(cin>>n>>m){
		for(int i=0;i<n;++i)cin>>Map[i];
		for(int i=0;i<n;++i){
			for(int j=0;j<m;++j){
				if(Map[i][j] == 'Z')start.x=i,start.y=j;
				if(Map[i][j] == 'S')start.ex=i,start.ey=j;
			}
		}
		start.time=0;
		int temp=BFS(++num);
		if(temp == -1)cout<<"Bad Luck!"<<endl;
		else cout<<temp<<endl;
	}
	return 0;
}


你可能感兴趣的:(hdu2216之BFS)