zoukankan      html  css  js  c++  java
  • HDU 3085

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085

    Problem Description
    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

    题意:

    给定 $n imes m$ 的地图,现在地图上一些格子是墙,人不能通过。地图上有一个男孩和一个女孩,男孩一次能跑 $3$ 格,女孩一次能跑 $1$ 格。

    现在地图上有两个幽灵,每个格子的幽灵每秒钟可以扩散到曼哈顿距离不超过 $2$ 的所有格子,被扩散的格子都会变成有幽灵的,墙不能阻挡幽灵,它们会一直扩散直到遍布整个地图。

    假设每个单位时间内,都是幽灵先于男孩女孩移动。现在问你男孩和女孩在不碰到幽灵的前提下,能不能会和在一个格子。

    题解:

    可以分别构建两个队列,男孩和女孩同时进行BFS。并且对于男孩,一次BFS向四周扩展三层;对于女孩,一次BFS向四周扩展一层。

    AC代码:

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=805;
    const int dx[4]={0,1,0,-1};
    const int dy[4]={1,0,-1,0};
    
    int n,m,k;
    char mp[maxn][maxn];
    struct P{
        int x,y;
        P(int _x=0,int _y=0) {
            x=_x, y=_y;
        }
    }z[2];
    queue<P> G,M,T;
    
    bool ok(P p)
    {
        if(p.x<1 || p.x>n || p.y<1 || p.y>m) return 0;
        if(mp[p.x][p.y]=='X') return 0;
        if(abs(p.x-z[0].x)+abs(p.y-z[0].y)<=2*k) return 0;
        if(abs(p.x-z[1].x)+abs(p.y-z[1].y)<=2*k) return 0;
        return 1;
    }
    bool bfs(queue<P>& Q,int lim,char mk1,char mk2)
    {
        while(lim--)
        {
            T=Q;
            while(!T.empty())
            {
                P u=T.front(); T.pop(); Q.pop();
                if(!ok(u)) continue;
                for(int k=0;k<4;k++)
                {
                    P v=P(u.x+dx[k],u.y+dy[k]);
                    if(!ok(v) || mp[v.x][v.y]==mk1) continue;
                    if(mp[v.x][v.y]==mk2) return 1;
                    Q.push(v), mp[v.x][v.y]=mk1;
                }
            }
        }
        return 0;
    }
    
    int solve()
    {
        k=0;
        while(!G.empty() && !M.empty())
        {
            k++;
            bool flag1=bfs(M,3,'M','G');
            bool flag2=bfs(G,1,'G','M');
            if(flag1||flag2) return k;
        }
        return -1;
    }
    
    int main()
    {
        int Kase;
        cin>>Kase;
        while(Kase--)
        {
            scanf("%d%d",&n,&m);
            while(!G.empty()) G.pop();
            while(!M.empty()) M.pop();
            for(int i=1,t=0;i<=n;i++)
            {
                scanf("%s",mp[i]+1);
                for(int j=1;j<=m;j++)
                {
                    if(mp[i][j]=='M') M.push(P(i,j));
                    if(mp[i][j]=='G') G.push(P(i,j));
                    if(mp[i][j]=='Z') z[t++]=P(i,j);
                }
            }
            printf("%d
    ",solve());
        }
    }
  • 相关阅读:
    Saltstack的部署及其详解
    SSH免密远程登陆及详解
    LVS-负载均衡集群部署
    nginx的平滑升级
    MFS分布式文件系统管理
    cacti监控部署与配置
    Nagios部署与配置
    zabbix分布式监控的部署与win被控端
    Fail2防止sshd暴力破解
    C# 发邮件
  • 原文地址:https://www.cnblogs.com/dilthey/p/10029673.html
Copyright © 2011-2022 走看看