zoukankan      html  css  js  c++  java
  • BFS:HDU3085-Nightmare Ⅱ(双向BFS)

    Nightmare Ⅱ

    Time Limit: 2000/1000 MS (Java/Others)    

    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 886    

    Accepted Submission(s): 185

    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
     

    Author
    二日月
     

    Source

    n*m地图上有

    ‘. ’:路

    ‘X':墙

    ’Z':鬼,每秒蔓延2个单位长度,可以穿墙,共两个,每秒开始时鬼先动

    ‘M’:一号,每分钟可移动3个单位长度

    ‘G’:二号,每分钟课移动1个单位长度



    解题心得:
    1、这个题很明显的一个双向bfs,可能一开始会觉得是三个,因为还有一个幽灵的,但是幽灵每次走两格的距离,就可以直接按照x轴与y轴的距离的和step*2相比较看是否可以被鬼抓到。还有一点很重要的是每次都是鬼先走,所以在每次移动之前都要先判断一下。
    2、这个题有个特点就是每一秒boy可以走三步,girl可以走一步,幽灵走两布,如果不写一个传递参数的函数代码的长度会很长,特别是在找bug和理框架的时候会很乱。所以平时在写一定功能的代码的时候要写成函数,并且多找找这些功能的共同点争取写成一个函数,思考怎么传递参数。
    3、关于双向bfs,可以有多种操作,可以就在地图上操作,将一个bfs走过的地方全部化为当前,判断一个是否走到另一个的地盘。也可以在标记上面找,当一个的标记走到另一个已经标记的时候就算相遇,反正有比较多的方法可以进行判断,视情况和题意而定。


    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 880;
    int n,m;
    int dir[4][2] = {1,0,-1,0,0,1,0,-1};
    char maps[maxn][maxn];
    struct node
    {
        int x,y;
    } now,Next,b,g,z[2];
    queue <node> q[2],qt;//q[0]为boy,q[1]为girl,qt为当前操作的队列
    int step = 0;
    void pre_maps()
    {
        //多组输入,清零
        while(!q[0].empty())
            q[0].pop();
        while(!q[1].empty())
            q[1].pop();
        while(!qt.empty())
            qt.pop();
            
        int k = 0;
        for(int i=0; i<n; i++)
            scanf("%s",maps[i]);
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
            {
                if(maps[i][j] == 'Z')
                {
                    z[k].x = i;
                    z[k].y = j;
                    k++;
                }
                if(maps[i][j] == 'M')
                {
                    b.x = i;
                    b.y = j;
                }
                if(maps[i][j] == 'G')
                {
                    g.x = i;
                    g.y = j;
                }
            }
    }
    
    bool check(node a)
    {
        if(a.x <0 || a.y <0 || a.x >= n || a.y >= m)//超出地图直接返回
            return true;
        for(int i=0; i<2; i++)
        {
            if(maps[a.x][a.y] == 'X' || (abs(a.x-z[i].x)+abs(a.y-z[i].y)) <= 2*step)//是墙,或者被鬼抓到返回
                return true;
        }
        return false;
    }
    bool bfs(int k,int num,char start,char End)
    {
        qt = q[k];//用来表示当前层的搜索,队列也可以直接赋值
        for(int i=0; i<num; i++)
        {
            while(!qt.empty())
            {
                now = qt.front();
                qt.pop();
                q[k].pop();
                if(check(now)) continue;
                for(int j=0; j<4; j++)
                {
                    Next.x = now.x + dir[j][0];
                    Next.y = now.y + dir[j][1];
                    if(check(Next)) continue;
                    if(maps[Next.x][Next.y] == start) continue;//已经走过了
                    if(maps[Next.x][Next.y] == End)//两个bfs相遇
                        return true;
                    maps[Next.x][Next.y] = start;//走过的地点直接标记
                    q[k].push(Next);
                }
            }
            qt = q[k];//将下一层的队列赋值给当前层
        }
        return false;
    }
    
    int solve()
    {
        bool flag1 = false,flag2 = false;
        step = 0;
        q[0].push(b);
        q[1].push(g);
        while(!q[0].empty() && !q[1].empty())
        {
            step++;
            
            flag1 = bfs(0,3,'M','G');//boy的bfs
            flag2 = bfs(1,1,'G','M');//girl的bfs
            if(flag1 || flag2)//当有一方相遇了
                return step;
        }
        return -1;//没有找到
    }
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d%d",&n,&m);
            pre_maps();
            printf("%d
    ",solve());
        }
    }
    
    



  • 相关阅读:
    多线程之异步操作
    AppDomain
    多线程之线程同步
    webServices与Web服务
    webParts与Web部件
    xhtmlConformance与xhtml脚本呈现
    webControls与客户端脚本路径
    urlMappings与URL映射
    trace与代码跟踪服务
    siteMap与站点导航
  • 原文地址:https://www.cnblogs.com/GoldenFingers/p/9107343.html
Copyright © 2011-2022 走看看