HDU2216:Game III(BFS)

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!


提示:其实可以近似理解成两个相互搜索对方,搜索规则则是两人的移动位子相对(但S遇到障碍物就只能留在原位置不动),比较巧妙的地方是需要开一个四维数组记录移动的路线(因为是两个人,所以多了一组XY坐标)。
PS:刚才开始做的时候没留意到, 两人的起始位置务必记为“.”(空地)。

#include<stdio.h>
#include<queue>
#include<string>
using namespace std;

char map[25][25];
int vis[21][21][21][21];
int zx,zy,sx,sy,n,m;
int dir[4][2]={1,0,-1,0,0,1,0,-1};

struct node
{
	int sx,sy,zx,zy,step;
};

int check(int x,int y)
{
	if(x<0||y<0||x>=n||y>=m||map[x][y]=='X')
		return 1;
	return 0;
}

int bfs()
{
	queue<node>Q;
	node a,nex;
	memset(vis,0,sizeof(vis));
	a.zx=zx;
	a.zy=zy;
	a.sx=sx;
	a.sy=sy;
	vis[zx][zy][sx][sy]=1;
	a.step=0;
	Q.push(a);
	while(!Q.empty())
	{
		a=Q.front();
		Q.pop();
		if((a.zx==a.sx&&a.zy==a.sy)||(a.zx+1==a.sx&&a.zy==a.sy)||(a.zx-1==a.sx&&a.zy==a.sy)||(a.zx==a.sx&&a.zy-1==a.sy)||(a.zx==a.sx&&a.zy+1==a.sy))
			return a.step;
		for(int i=0;i<4;++i)
		{
			nex.zx=a.zx+dir[i][0];
			nex.zy=a.zy+dir[i][1];
			if(check(nex.zx,nex.zy))
				continue;
			nex.sx=a.sx-dir[i][0];
			nex.sy=a.sy-dir[i][1];
			if(check(nex.sx,nex.sy))
			{
				nex.sx=a.sx;
				nex.sy=a.sy;
			}
			if(vis[nex.zx][nex.zy][nex.sx][nex.sy])
				continue;
			vis[nex.zx][nex.zy][nex.sx][nex.sy]=1;
			nex.step=a.step+1;
			//printf("Z:%d%d S:%d%d step:%d\n",nex.zx,nex.zy,nex.sx,nex.sy,nex.step);
			Q.push(nex);
		}
	}
	return -1;
}

int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		for(int i=0;i<n;++i)
		{
		scanf("%s",map[i]);
		for(int j=0;map[i][j];++j)
			if(map[i][j]=='Z')
			{
				map[i][j]='.';
				zx=i;
				zy=j;
			}
			else if(map[i][j]=='S')
			{
				map[i][j]='.';
				sx=i;
				sy=j;
			}
		}
		//printf("s:%d%d z:%d%d\n",sx,sy,zx,zy);
		int ans=bfs();
		if(ans<0)
			puts("Bad Luck!");
		else
			printf("%d\n",ans);
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值