HDU -2216 Game III(BFS)

Game III

Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others)

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!

题意:Z和S走的是反方向,最少几步相遇。例一:S向上走,Z向下走但是走不了,此时相遇,ans=1

注意用4维数组标记状态,因为S不动而Z动这也是一种状态,如果用2维分别标记S和Z,那么这种状态会被跳过

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<algorithm>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;

int n, m;
char map[25][25];
int dir[4][2]= {1,0,0,-1,0,1,-1,0};//4个方向
int vis[25][25][25][25];//标记数组

struct node
{
    int xz, yz, xs, ys, step;
    node(int xz,int yz,int xs,int ys,int step):xz(xz),yz(yz),xs(xs),ys(ys),step(step) {}//构造函数,push()用着方便
};

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

int bfs(int xz, int yz, int xs, int ys)
{
    queue<node>s;
    while(!s.empty())
        s.pop();
    s.push(node(xz, yz, xs, ys, 0));
    vis[xz][yz][xs][ys]=1;
    while(!s.empty())
    {
        node now=s.front();
        s.pop();
        if((abs(now.xz-now.xs)+abs(now.yz-now.ys))<=1)//注意括号的位置,先两个(abs()+abs())相加再和1比较,这里括号位置错了一直WA?
            return now.step;
        for(int i=0; i<4; i++)
        {
            int x1, y1, x2, y2, step;
            x1=now.xz+dir[i][0], y1=now.yz+dir[i][1];//一个'+'一个'-'就可以实现反方向移动
            x2=now.xs-dir[i][0], y2=now.ys-dir[i][1];
            if(check(x1, y1))//如果Z越界或艹墙,换方向
                continue;
            if(check(x2, y2))//到达这里时Z已经移动了,属于新状态,判断S,移动违法就退回
                x2=now.xs,y2=now.ys;
            if(vis[x1][y1][x2][y2])
                continue;
            vis[x1][y1][x2][y2]=1;
            step=now.step+1;
            s.push(node(x1,y1,x2,y2,step));
        }
    }
    return -1;
}

int main()
{
    while(scanf("%d%d", &n, &m)!=EOF)
    {
        int xz, yz, xs, ys;
        for(int i=0; i<n; i++)
        {
            scanf("%s", map[i]);
            for(int j=0; j<m; j++)
            {
                if(map[i][j]=='Z')
                    xz=i, yz=j;
                if(map[i][j]=='S')
                    xs=i, ys=j;
            }
        }
        memset(vis, 0, sizeof(vis));
        int ans=bfs(xz, yz, xs, ys);
        if(ans==-1)
            printf("Bad Luck!\n");
        else
            printf("%d\n", ans);
    }
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值