hdu 3085 Nightmare Ⅱ(双向bfs)

来源:互联网 发布:佛山cnc编程招聘 编辑:程序博客网 时间:2024/04/27 00:14

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
35 6XXXXXXXZ..ZXXXXXXXM.G.........5 6XXXXXXXZZ..XXXXXXXM.......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
11-1
 

题意:有M,G两人和鬼魂(Z)在n*m的方格内,M每秒走3步,G每秒走一步,鬼魂每秒走2步,问是否能

不遇到鬼魂下两人相遇,鬼魂可以穿墙(X),人不可以。初始鬼魂有2个。

注意:每秒开始时鬼先动





  1. #include <iostream>

    #include <stdio.h>

    #include <string.h>

    #include <queue>

    using namespace std;

    int dir[4][2]={{-1,0},{0,1},{0,-1},{1,0}};

    int n,m,step;

    char map[808][808];

    struct node

    {

        int x;

        int y;

        node(int x=-1,int y=-1)

        {

            this->x=x;

            this->y=y;

        }

    }G,M,Z[2];

    queue<node>q[3];

    bool ok(int x,int y)

    {

        for (int i=0; i<2; i++)

        {

            if(abs(x-Z[i].x)+abs(y-Z[i].y)<=2*step)

                return false;

        }

        return true;

    }

    void clear()

    {

        for (int i=0; i<3; i++)

        {

            while (!q[i].empty())

                q[i].pop();

        }

    }

    bool check(int x,int y)

    {

        if(x<0 || x>=n || y<0 || y>=m ||map[x][y]=='X')

            return false;

        return true;

    }

    bool bfs(int id,int num,char sour,char flag) //id代表队列编号,num:移动步数

    {

        node temp1,temp2;

        q[2]=q[id];

        for (int i=0; i<num; i++)

        {

            while (!q[2].empty())//对队列中的每个点进行处理

            {

                temp1=q[2].front();

                q[2].pop();

                q[id].pop();

                if (ok(temp1.x, temp1.y))//每次开始鬼先动,这里需要判断

                {

                    for (int j=0; j<4; j++)

                    {

                        temp2=temp1;

                        temp2.x+=dir[j][0];

                        temp2.y+=dir[j][1];

                        if(check(temp2.x, temp2.y) &&ok(temp2.x, temp2.y) && map[temp2.x][temp2.y]!=sour)

                        {

                            if(map[temp2.x][temp2.y]==flag)

                                return true;

                            map[temp2.x][temp2.y]=sour;

                            q[id].push(temp2);

                        }

                    }

                }

                

            }

            q[2]=q[id];

        }

        return false;

    }

    int solve()

    {

        step=0;

        clear();

        q[0].push(M);//m队列

        q[1].push(G);//g队列

        while (!q[0].empty() && !q[1].empty())

        {

            step++;

            if(bfs(0,3,'M','G')||bfs(1,1,'G','M'))

                return step;

        }

        return -1;

    }


    int main()

    {

        int t;

        scanf("%d",&t);

        int cnt;

        while (t--)

        {

            cnt=0;

            scanf("%d%d",&n,&m);

            getchar();

            for (int i=0; i<n; i++)

            {

                scanf("%s",map[i]);

                for (int j=0; j<m; j++)

                {

                    if(map[i][j]=='M')

                    {

                        M.x=i;

                        M.y=j;

                    }

                    else if(map[i][j]=='G')

                    {

                        G.x=i;

                        G.y=j;

                    }

                    else if(map[i][j]=='Z')

                    {

                        Z[cnt].x=i;

                        Z[cnt++].y=j;

                    }

                }

                getchar();

            }

            printf("%d\n",solve());

        }

        return 0;

    }



0 0
原创粉丝点击