HDU 2216 Game III

本文介绍了一个迷宫游戏挑战,玩家Zjt需要在一个特殊规则的迷宫中找到Sara。迷宫中有墙壁和空地,Zjt每走一步,Sara会向相反方向移动一步。任务是找出Zjt找到Sara所需的最少步数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=25;
int n,m,bx,by,ex,ey;
char s[N];
int mp[N][N];
int f[N][N][N][N];

struct point
{
    int a,b,c,d;
    point(int a=0,int b=0,int c=0,int d=0):a(a),b(b),c(c),d(d){};
};

int d[4][2]={{1,0},{-1,0},{0,1},{0,-1}};

int main()
{
    while (~scanf("%d%d",&n,&m))
    {
        memset(mp,0,sizeof(mp));
        memset(f,-1,sizeof(f));
        for (int i=1;i<=n;i++)
        {
            scanf("%s",s+1);
            for (int j=1;j<=m;j++)
            {
                if (s[j]=='Z') {s[j]='.'; bx=i; by=j; }
                if (s[j]=='S') {s[j]='.'; ex=i; ey=j; }
                mp[i][j]=s[j]=='.';
            }
        }
        queue<point> p; 
        p.push(point(bx,by,ex,ey));
        f[bx][by][ex][ey]=0;
        int flag=0;
        while (!p.empty())
        {
            point q=p.front(); p.pop();
            if (abs(q.a-q.c)+abs(q.b-q.d)<=1)
            {
                printf("%d\n",f[q.a][q.b][q.c][q.d]);
                flag=1; break;
            }
            for (int i=0;i<4;i++)
            {
                int X=q.a+d[i][0],Y=q.b+d[i][1];
                if (!mp[X][Y]) continue;
                int XX=q.c+d[i^1][0],YY=q.d+d[i^1][1];
                if (mp[XX][YY])
                {
                    if (f[X][Y][XX][YY]==-1)
                    {
                        p.push(point(X,Y,XX,YY));
                        f[X][Y][XX][YY]=f[q.a][q.b][q.c][q.d]+1;
                    }
                }
                else 
                {
                    XX=q.c; YY=q.d;
                    if (f[X][Y][XX][YY]==-1)
                    {
                        p.push(point(X,Y,XX,YY));
                        f[X][Y][XX][YY]=f[q.a][q.b][q.c][q.d]+1;
                    }
                }
            }
        }
        if (!flag) printf("Bad Luck!\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值