• HDU 3085 Nightmare Ⅱ


    Nightmare Ⅱ

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 718    Accepted Submission(s): 135


    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
     
    Recommend
    lcy
     
    思路:双向广搜
     
    代码:
    #include <iostream>
    #include <cstring>
    #include <cstring>
    #include <cstdio>
    #include <cstdlib>
    #include <queue>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    int t,n,m;
    char map[810][810];
    int the_woman_x,the_woman_y;
    int the_man_x,the_man_y;
    int ghost_one[2],ghost_two[2];
    int visit_woman[810][810],visit_man[810][810];
    int the_last_step;
    int the_last_flag;
    int move[4][2] = {1,0,-1,0,0,1,0,-1};
    struct Node
    {
        int x,y;
    }q_one[100000],q_two[100000];
    bool isghost(int x,int y,int step)
    {
        if(x < 0 || x >= n || y < 0 || y >= m || map[x][y] == 'X')
           return false;
        int g_one = 0,g_two = 0;
        g_one = (abs(x - ghost_one[0]) + abs(y - ghost_one[1]));
        g_two = (abs(x - ghost_two[0]) + abs(y - ghost_two[1]));
        if(step * 2 >= g_one || step * 2 >= g_two)
            return false;
        return true;
    }
    void BFS()
    {
        int head_man = 0,head_woman = 0,tail_man = -1,tail_woman = -1;
        int step = 1;
        Node top;top.x = the_man_x;top.y = the_man_y;
        q_one[++ tail_man] = top;
        Node cur;cur.x = the_woman_x;cur.y = the_woman_y;
        q_two[++ tail_woman] = cur;
        while(1)
        {
            int flag = 0;
            for(int i = 1;i <= 3;i ++)
            {
                int temp = tail_man;
                while(head_man <= temp)
                {
                    flag = 1;
                    Node now;
                    now = q_one[head_man];
                    head_man ++;
                    int x = now.x;int y = now.y;
                    if(!isghost(x,y,step)) continue ;
                    for(int j = 0;j < 4;j ++)
                    {
                        int nowx = now.x + move[j][0];
                        int nowy = now.y + move[j][1];
                        if(!isghost(nowx,nowy,step) || visit_man[nowx][nowy] == 1)
                            continue ;
                        if(visit_woman[nowx][nowy] == 1)
                        {
                            the_last_step = step;
                            return ;
                        }
                        visit_man[nowx][nowy] = 1;
                        Node man;man.x = nowx;man.y = nowy;
                        q_one[++ tail_man] = man;
                    }
    
                }
            }
            int temp = tail_woman;
            while(head_woman <= temp)
            {
                flag = 1;
                Node now = q_two[head_woman];
                head_woman ++;
                int x = now.x;int y = now.y;
                if(!isghost(x,y,step)) continue ;
                for(int j = 0;j < 4;j ++)
                {
                    int nowx = now.x + move[j][0];
                    int nowy = now.y + move[j][1];
                    if(!isghost(nowx,nowy,step) || visit_woman[nowx][nowy] == 1)
                        continue ;
                    if(visit_man[nowx][nowy] == 1)
                    {
                        the_last_step = step;
                        return ;
                    }
                    visit_woman[nowx][nowy] = 1;
                    Node woman;woman.x = nowx;woman.y = nowy;
                    q_two[++ tail_woman] = woman;
                }
            }
            step ++;
            if(flag == 0)
               break ;
        }
        return ;
    }
    int main()
    {
        scanf("%d",&t);
        while(t --)
        {
            scanf("%d%d",&n,&m);
            memset(map,0,sizeof(map));
            memset(ghost_one,0,sizeof(ghost_one));
            memset(ghost_two,0,sizeof(ghost_two));
            memset(visit_woman,0,sizeof(visit_woman));
            memset(visit_man,0,sizeof(visit_man));
            memset(q_one,0,sizeof(q_one));
            memset(q_two,0,sizeof(q_two));
            int flag = 0;
            for(int i = 0;i < n;i ++)
            {
                scanf("%s",map[i]);
                for(int j = 0;j < m;j ++)
                {
                    if(map[i][j] == 'Z')
                    {
                        if(flag == 0)
                        {
                            ghost_one[0] = i;ghost_one[1] = j;
                            flag ++;
                        }
                        else
                        {
                            ghost_two[0] = i;ghost_two[1] = j;
                        }
    
                    }
                    if(map[i][j] == 'M')
                    {
                        the_man_x = i;the_man_y = j;
                        visit_man[i][j] = 1;
                    }
                    if(map[i][j] == 'G')
                    {
                        the_woman_x = i;the_woman_y = j;
                        visit_woman[i][j] = 1;
                    }
                }
            }
            the_last_step = 0;
            BFS();
            if(the_last_step == 0)
                printf("-1
    ");
            else
                printf("%d
    ",the_last_step);
        }
        return 0;
    }
  • 相关阅读:
    Biba模型简介
    Fragment 与 Activity 通信
    小米2S 连接Ubuntu Android Studio
    【转】Android 实现“再按一次退出程序”
    取消 EditText 自动聚焦弹出输入法界面
    为Android Studio 项目手动下载gradle
    【转】Java读取文件方法大全
    sudo: /etc/sudoers 的模式为 0551,应为 0440
    Win7 下硬盘安装Linux Mint 17
    Linux Versus Windows, Ubuntu/Mint V XP/Vista/7
  • 原文地址:https://www.cnblogs.com/GODLIKEING/p/3356406.html
Copyright © 2020-2023  润新知