hdu 2216 Game III

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!

简单的bfs 题目 写的时候 不知道哪里坑了 查了半天错

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<math.h>
using namespace std;
char map[30][30];
int biaoji[30][30][30][30];
int n,m;
int xx1,xx2,yy1,yy2;
int dir[4][2]={1,0,0,1,-1,0,0,-1};
struct node
{
	int x1;
	int y1;
	int x2;
	int y2;
	int bushu;
};
int sou()
{
	int temp,i;
	queue<node>q;
	node xian,xia;
	memset(biaoji,0,sizeof(biaoji));
	xian.x1=xx1;
	xian.y1=yy1;
	xian.x2=xx2;
	xian.y2==yy2;
	xian.bushu=0;
	q.push(xian);
	biaoji[xx1][yy1][xx2][yy2]=1;
	while(!q.empty())
	{
		xian=q.front();
		q.pop();
		if(xian.x1==xian.x2&&xian.y1==xian.y2) return xian.bushu;
		if(abs(xian.x1-xian.x2)==1&&xian.y1==xian.y2)  return xian.bushu;
		if(abs(xian.y1-xian.y2)==1&&xian.x1==xian.x2)  return xian.bushu;
		for(i=0;i<4;i++)
		{
			temp=i+2;
			temp%=4;
			xia.x1=xian.x1+dir[i][0];
			xia.y1=xian.y1+dir[i][1];
			xia.x2=xian.x2+dir[temp][0];
			xia.y2=xian.y2+dir[temp][1];
			
			if(xia.x1<0||xia.x1>=n||xia.y1<0||xia.y1>=m||map[xia.x1][xia.y1]=='X')
			continue;
			if(xia.x2<0||xia.x2>=n||xia.y2<0||xia.y2>=m||map[xia.x2][xia.y2]=='X')
			{
				xia.x2=xian.x2;
				xia.y2=xian.y2;
			}
			if(biaoji[xia.x1][xia.y1][xia.x2][xia.y2])  continue;
			
			biaoji[xia.x1][xia.y1][xia.x2][xia.y2]=1;
			xia.bushu=xian.bushu+1;
			q.push(xia);
		}
	}
	return -1;
}

int main()
{
	int t;
	int i,j;
	while(~scanf("%d%d",&n,&m))
	{
		
		for(i=0;i<n;i++)
		{
			scanf("%s",map[i]);
		}
		for(i=0;i<n;i++)
		{
			for(j=0;j<m;j++)
			{
				if(map[i][j]=='Z')  
				{
					map[i][j]='.';
					xx1=i;
					yy1=j;
				}
				else if(map[i][j]=='S')
				{
					map[i][j]='.';
					xx2=i;
					yy2=j;
				}
			}
		}
		t=sou();
		if(t==-1)  printf("Bad Luck!\n");
		else printf("%d\n",t);
	}
	return 0;
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值