hdu 2216 Game III

Game III

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


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!
 

Author
zjt
 

Recommend
lcy   |   We have carefully selected several similar problems for you:   1547  2215  2821  2822  2782 



个人表示这个题目还是很有意思的,其中比较费心思的是怎么样来标记每种状态,后来看了别人的才想通,应该用一个四维数组就可以了,这样每个状态就可以保证能够是不是出现过了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<math.h>
using namespace std;
#define M 30
int maze[M][M][M][M];
char map[M][M];
int n,m,sx,sy,ex,ey;
struct node
{
    int ax,ay;
    int bx,by;
    int step;
}ua,ub;
int dir[8][2]={{1,0},{-1,0},{0,1},{0,-1},{-1,0},{1,0},{0,-1},{0,1}};
int judge(node a)
{
     int x,y;
     x=fabs(a.bx-a.ax);
     y=fabs(a.by-a.ay);
     if(x+y<2)
     return 1;
     else
     return 0;
}
int check(int a,int b)
{
 if(a>=0&&a<n&&b>=0&&b<m&&map[a][b]!='X')
 return 1;
 return 0;
}
int bfs()
{
    queue<node>q;
    int x1,y1,x2,y2;
    int i,j;
    memset(maze,0,sizeof(maze));
    ua.ax=sx;
    ua.ay=sy;
    ua.bx=ex;
    ua.by=ey;
    ua.step=0;
    maze[sx][sy][ex][ey]=1;//以两点的位置作为标记,一旦走过就不再走了
    q.push(ua);
    while(!q.empty())
    {
        ua=q.front();
        q.pop();
        if(judge(ua))
        {
            return ua.step;
        }
        for(i=0;i<4;i++)
        {

            ub=ua;
            ub.ax=ub.ax+dir[i][0];
            ub.ay=ub.ay+dir[i][1];
            ub.step++;
            //z节点走到了某一个位置,下面判断是否符合条件。
            if(check(ub.ax,ub.ay))//现在z所在的点符合了条件
            {
                if(check(ub.bx+dir[i+4][0],ub.by+dir[i+4][1]))
                {//s点进行了移动,进行计算标记
                    ub.bx=ub.bx+dir[i+4][0];
                    ub.by=ub.by+dir[i+4][1];
                    if(maze[ub.ax][ub.ay][ub.bx][ub.by]==0)
                    {
                        maze[ub.ax][ub.ay][ub.bx][ub.by]=1;
                        q.push(ub);
                    }





                }
                else
                {//s点没有进行移动,但是目前的情况仍旧未曾发生过即可。

                     if(maze[ub.ax][ub.ay][ub.bx][ub.by]==0)
                    {
                        maze[ub.ax][ub.ay][ub.bx][ub.by]=1;
                        q.push(ub);
                    }





                }







            }







        }














    }






    return -1;


}
int main()
{
    int i,j,k;
    while(scanf("%d%d",&n,&m)!=EOF)
    {

        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')
                {
                    sx=i;
                    sy=j;
                }
                else
                if(map[i][j]=='S')
                {
                    ex=i;
                    ey=j;
                }
            }
        }
        int ans=bfs();
        if(ans!=-1)
        {
           printf("%d\n",ans);
        }
        else
        {
            printf("Bad Luck!\n");
        }






    }
    return 0;
}










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值