HDU 3085 Nightmare Ⅱ(初学双向BFS)

本文介绍了一个迷宫逃脱问题的算法解决思路,通过双向BFS搜索,模拟男主角与女友在迷宫中躲避鬼魂并试图相会的过程。每秒男主角可移动3步,女友移动1步,而鬼魂则能向四周扩散2格。文章详细解释了如何通过层次遍历来模拟移动,并检查每一步是否安全,最终计算出两人成功相遇所需的最短时间。

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

题面

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.

Sample Input

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

Sample Output

1
1
-1

题目链接

HDU_3085

参考链接

HDU3085 Author:Salvete

题意

我和我的女朋友被困迷宫,我和我女朋友不可以穿墙,鬼可以穿墙。

我每秒走3步,女朋友每秒走1步,鬼每秒向上下左右扩展两个。即:

     
     
  Z  
     
     

 

  Z  
  Z  
ZZZZZ
  Z  
  Z  

 问我和女朋友最少多长时间(在不被鬼抓到的情况下)可以相遇?

分析

分别以我和女朋友为起点,进行bfs,为了模拟走一步,则对bfs搜索树,进行一层的遍历。

一秒走三步,则在一秒内,遍历3层。

程序说明

bool is_kill(Node now)
判断now节点是否合法,是否会被鬼杀掉
bool bfs(int person)/**I:person==0  Girlfriend:person==1**/
person走一步,对此时q进行一层遍历(层次遍历)
int work()
计算结果
void read()
初始化+读入

程序

#include<stdio.h>
#include<queue>
#include<math.h>
#include<cmath>
#include<string.h>
using namespace std;
#define maxn 800
int time;
struct Node
{
    int x,y;
    Node(int X,int Y):x(X),y(Y){}
    Node(){}
    void change(int X,int Y){x=X,y=Y;}
}I,girl,ghost[2];

queue<Node>q[2];

int zx[]={0,0,1,-1};
int zy[]={1,-1,0,0};
bool visit[2][maxn][maxn];
char MAP[maxn][maxn];
int n,m;

bool is_kill(Node now)
{
    if(now.x<0||now.x>=n||now.y<0||now.y>=m||MAP[now.x][now.y]=='X')
        return true;
    for(int i=0;i<=1;i++)
    {
        if(abs(now.x-ghost[i].x)+abs(now.y-ghost[i].y)<=time*2)
            return true;
    }
    return false;
}

bool bfs(int person)/**I:person==0  Girlfriend:person==1**/
{
    Node now,next;
    int Size=q[person].size();
    for(int i=0;i<Size;i++)
    {
        now=q[person].front();
        q[person].pop();
        if(is_kill(now))
            continue;
        for(int j=0;j<4;j++)
        {
            next=now;
            next.x+=zx[j];
            next.y+=zy[j];
            if((!is_kill(next))&&!visit[person][next.x][next.y])
            {
                if(visit[1-person][next.x][next.y])
                    return true;
                visit[person][next.x][next.y]=true;
                q[person].push(next);
            }
        }
    }
    return false;
}

int work()
{
    for(int i=0;i<2;i++)
        while(!q[i].empty())
            q[i].pop();
    memset(visit,0,sizeof(visit));
    q[0].push(I);
    q[1].push(girl);
    visit[0][I.x][I.y]=visit[1][girl.x][girl.y]=true;
    time=0;
    while(!q[0].empty()||!q[1].empty())
    {
        time++;
        if(bfs(0))
            return time;
//        printf("time=%d\n",time);
        if(bfs(0))
            return time;
//        printf("time=%d\n",time);
        if(bfs(0))
            return time;
//        printf("time=%d\n",time);
        if(bfs(1))
            return time;
    }
    return -1;
}

void read()
{
    memset(MAP,'\0',sizeof(MAP));
    scanf("%d%d",&n,&m);
    ghost[0].change(-1,-1);
    ghost[1].change(-1,-1);
    for(int i=0;i<n;i++)
    {
        scanf("%s",MAP[i]);
        for(int j=0;j<m;j++)
        {
            if(MAP[i][j]=='M')
                I.change(i,j);
            else if(MAP[i][j]=='G')
                girl.change(i,j);
            else if(MAP[i][j]=='Z')
            {
                if(ghost[0].x==-1)
                    ghost[0].change(i,j);
                else
                    ghost[1].change(i,j);
            }
        }
    }
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        read();
        printf("%d\n",work());
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值