J - Nightmare Ⅱ(DBFS)

这是一个关于编程算法的问题,描述了一个迷宫中男孩(M)、女孩(G)和两个恶灵(Z)的场景。男孩和女孩每次可以向四个方向移动,男孩每秒移动三步,女孩移动一步,而恶灵会以两步的速度扩散占据周围空格。目标是判断男孩和女孩能否在恶灵到达前相遇。输入包含迷宫的大小和布局,输出是他们成功相遇的最短时间,或者如果无法相遇则输出-1。提供的代码实现了广度优先搜索算法来解决这个问题。
摘要由CSDN通过智能技术生成

Last night, little erriyue had a horrible nightmare. He dreamed that he and his girl friend were trapped in a big maze separately. More terribly, there are two ghosts in the maze. They will kill the people. Now little erriyue wants to know if he could find his girl friend before the ghosts find them.
You may suppose that little erriyue and his girl friend can move in 4 directions. In each second, little erriyue can move 3 steps and his girl friend can move 1 step. The ghosts are evil, every second they will divide into several parts to occupy the grids within 2 steps to them until they occupy the whole maze. You can suppose that at every second the ghosts divide firstly then the little erriyue and his girl friend start to move, and if little erriyue or his girl friend arrive at a grid with a ghost, they will die.
Note: the new ghosts also can devide as the original ghost.

Input
The input starts with an integer T, means the number of test cases.
Each test case starts with a line contains two integers n and m, means the size of the maze. (1<n, m<800)
The next n lines describe the maze. Each line contains m characters. The characters may be:
‘.’ denotes an empty place, all can walk on.
‘X’ denotes a wall, only people can’t walk on.
‘M’ denotes little erriyue
‘G’ denotes the girl friend.
‘Z’ denotes the ghosts.
It is guaranteed that will contain exactly one letter M, one letter G and two letters Z.

Output
Output a single integer S in one line, denotes erriyue and his girlfriend will meet in the minimum time S if they can meet successfully, or output -1 denotes they failed to meet.

这个题只需要跑男孩和女朋友就行,直接判断与鬼的距离,不需要对鬼进行搜索

3
5 6
XXXXXX
XZ..ZX
XXXXXX
M.G...
......
5 6
XXXXXX
XZZ..X
XXXXXX
M.....
..G...

10 10
..........
..X.......
..M.X...X.
X.........
.X..X.X.X.
.........X
..XX....X.
X....G...X
...ZX.X...
...Z..X..X
1
1
-1
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <queue>
using namespace std;
int off[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};
char str[800][800];
int used[2][800][800];//标记是否有鬼
int n,m,step;
struct node
{
    int x,y;
} start,zhan[2];
queue<node> q[2];
int judge(node b)
{
    if(b.x<0 || b.y<0 || b.x>=n || b.y>=m)
        return 0;
    if(str[b.x][b.y]=='X')
        return 0;
    //曼哈顿距离判断问题,鬼不用广搜,直接判断距离即可
    if((abs(b.x-zhan[0].x)+abs(b.y-zhan[0].y))<=2*step)
        return 0;
    if((abs(b.x-zhan[1].x)+abs(b.y-zhan[1].y))<=2*step)
        return 0;
    return 1;
}
int bfs(int w)
{
    node next,now;
    int sum=q[w].size();
    while(sum--)
    {
        now=q[w].front();
        q[w].pop();
        if(judge(now)==0)
            continue;
        for(int i=0; i<4; i++)
        {
            next.x=now.x+off[i][0];
            next.y=now.y+off[i][1];
            if(judge(next)==0)
                continue;
            if(used[w][next.x][next.y]==0)
            {
                if(used[1-w][next.x][next.y]==1)
                {
                    return 1;//会和成功
                }
                used[w][next.x][next.y]=1;
                q[w].push(next);
            }
        }
    }
    return 0;
}
int solve()
{
    step=0;
    while((q[0].empty()==0) || (q[1].empty()==0))
    {
        step++;
        if(bfs(0)==1)
        {
            return step;
        }
        if(bfs(0)==1)
        {
            return step;
        }
        if(bfs(0)==1)
        {
            return step;
        }
        if(bfs(1)==1)
        {
            return step;
        }
    }
    return -1;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int i,j,cnt;
        scanf("%d%d",&n,&m);
        for(int i=0; i<n; i++)
            scanf("%s",str[i]);
        while(!q[0].empty())
            q[0].pop();
        while(!q[1].empty())
            q[1].pop();
        memset(used,0,sizeof(used));
        cnt=0;
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
            {
                if(str[i][j]=='G')
                {
                    start.x=i;
                    start.y=j;
                    q[1].push(start);
                    used[1][i][j]=1;
                }
                if(str[i][j]=='M')
                {
                    start.x=i;
                    start.y=j;
                    q[0].push(start);
                    used[0][i][j]=1;
                }
                if(str[i][j]=='Z')
                {
                    zhan[cnt].x=i;
                    zhan[cnt].y=j;
                    cnt++;
                }
            }
        printf("%d\n",solve());
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Prime me

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值