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和Z同在一迷宫,其中一个人走一步,另外一个人就朝相反的方向走一步,问多少步能够到达相同位置或者两人相邻

思路:开一个四维数组记录,要注意的是两人的初始位置要置为“.”空地

本来我一开始是开了两个三维数组的,虽然过了样例还是A不了,求教哪位高手用三维做的指点下

 

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

int m,n;
int z1,z2,s1,s2;
char map[25][25];
int vis[25][25][25][25];
int to[4][2] = {1,0,-1,0,0,-1,0,1};//Z的走法
int to2[4][2] = {-1,0,1,0,0,1,0,-1};//S走相反路线

struct node
{
    int x1,x2,y1,y2,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 judge(int x,int x1,int y,int y1)//符合条件的状况
{
    if(x == x1 && y == y1)
        return 1;
    if(x == x1+1 && y == y1)
        return 1;
    if(x == x1-1 && y == y1)
        return 1;
    if(x == x1 && y == y1-1)
        return 1;
    if(x == x1 && y == y1+1)
        return 1;
    return 0;
}

int bfs()
{
    int i;
    queue<node> Q;
    node a,next;
    memset(vis,0,sizeof(vis));
    a.x1 = z1;
    a.x2 = s1;
    a.y1 = z2;
    a.y2 = s2;
    a.step = 0;
    vis[z1][z2][s1][s2] = 1;//四维数组记录行走状况
    Q.push(a);
    while(!Q.empty())
    {
        a = Q.front();
        Q.pop();
        if(judge(a.x1,a.x2,a.y1,a.y2))
            return a.step;
        for(i = 0; i<4; i++)
        {
            next = a;
            next.x1 = a.x1+to[i][0];
            next.y1 = a.y1+to[i][1];
            next.x2 = a.x2+to2[i][0];
            next.y2 = a.y2+to2[i][1];
            if(check(next.x1,next.y1))
                continue;
            if(check(next.x2,next.y2))
            {
                next.x2 = a.x2;
                next.y2 = a.y2;
            }
            if(vis[next.x1][next.y1][next.x2][next.y2])
                continue;
            vis[next.x1][next.y1][next.x2][next.y2] = 1;
            next.step = a.step+1;
            Q.push(next);
        }
    }
    return 0;
}

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        int i,j;
        for(i = 0; i<n; i++)
        {
            scanf("%s",map[i]);
            for(j = 0; map[i][j]; j++)
            {
                if(map[i][j] == 'Z')
                {
                    map[i][j] == '.';//初始位置置为空地
                    z1 = i;
                    z2 = j;
                }
                else if(map[i][j] == 'S')
                {
                    map[i][j] == '.';
                    s1 = i;
                    s2 = j;
                }
            }
        }
        int ans;
        ans = bfs();
        if(ans)
            printf("%d\n",ans);
        else
            printf("Bad Luck!\n");
    }

    return 0;
}


 

你可能感兴趣的:(ACM,HDU,杭电,bfs)