HDU 3085 —— Nightmare Ⅱ 双向BFS

原题:http://acm.hdu.edu.cn/showproblem.php?pid=3085

题意:

' X ' 表示墙;' . ' 表示空地;' Z ' 表示鬼(鬼有两只);

M每秒走三步;G每秒走一步;鬼每秒分裂,每次分裂距离鬼两步距离的地方都会被鬼覆盖,且鬼可以穿墙(当然鬼的分身也可分裂);

每秒鬼先分裂,然后M 和 G才走;

问在不遇到鬼的情况下,至少过几秒M和G可以相遇;


思路:

双向BFS,每秒M走向G以及G走向M;因为两个人在一秒钟内是同时走的,所以每次对一个人BFS时将其走过的路都标记为其本身,另一个人只要走到其中的任意一个就算是相遇了;

用曼哈顿距离判断会不会遇到鬼,即两只鬼(最原始的鬼)和人的距离在当前时间下能否走到(鬼的分裂就相当于鬼每秒走两步);



#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
using namespace std;
const int maxn = 1000;
char map[maxn][maxn];
int cas, n, m;
int mx, my, gx, gy;
int step;
int zz[5][2];
int f[4][2] = {-1, 0, 0, -1, 1, 0, 0, 1};

struct node
{
    int x, y;
};

bool Dis(int x, int y)
{
    int d1 = abs(x - zz[0][0]) + abs(y - zz[0][1]);
    int d2 = abs(x - zz[1][0]) + abs(y - zz[1][1]);
    if(d1 <= 2*step || d2 <= 2*step)    return false;
    return true;
}

queue<node>q[3];
bool bfs(int num, int k, char s, char e)
{
    q[2] = q[num];
    for(int t = 0;t<k;t++)
    {
        while(!q[2].empty())
        {
            node b = q[2].front();
            q[2].pop();
            q[num].pop();
            node c;
            if(!Dis(b.x, b.y))    continue;
            for(int i = 0;i<4;i++)
            {
                int nx = b.x + f[i][0];
                int ny = b.y + f[i][1];
                if(nx < 0 || ny < 0 || nx >= n || ny >= m || map[nx][ny] == 'X')    continue;
                if(!Dis(nx, ny) || map[nx][ny] == s)    continue;
                if(map[nx][ny] == e)    return true;
                map[nx][ny] = s;
                c.x = nx, c.y = ny;
                q[num].push(c);
            }            
        }
        q[2] = q[num];
    }
    return false;
}

void BFS()
{
    step = 0;
    for(int i = 0;i<3;i++)
    {
        while(!q[i].empty())
            q[i].pop();
    }
    node a;
    a.x = mx, a.y = my;
    q[0].push(a);
    a.x = gx, a.y = gy;
    q[1].push(a);
    while(!q[0].empty() && !q[1].empty())
    {
        step++;
        if(bfs(0, 3, 'M', 'G') || bfs(1, 1, 'G', 'M'))
        {
            printf("%d\n", step);
            return;
        }
    }
    printf("-1\n");
    return;
}

int main()
{
    scanf("%d", &cas);
    while(cas--)
    {
        scanf("%d%d", &n, &m);
        int cnt = 0;
        for(int i = 0;i<n;i++)
        {
            scanf("%s", map[i]);
            for(int j = 0;j<m;j++)
            {
                if(map[i][j] == 'M')
                mx = i, my = j;
                if(map[i][j] == 'G')
                gx = i, gy = j;
                if(map[i][j] == 'Z')
                {                    
                    zz[cnt][0] = i;
                    zz[cnt][1] = j;
                    cnt++;
                }
            }
        }
        BFS();
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值