1. 程式人生 > >HDU 3085 Nightmare Ⅱ【雙向bfs】

HDU 3085 Nightmare Ⅱ【雙向bfs】

Nightmare Ⅱ

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


 

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

題意:

M在一個迷宮中去找G

M一次可以走三步,G一次可以走一步。

M或G只要走到對方走到過的點上即算相遇。(意思是其中一個人可以原地不動)

迷宮中有很多鬼魂,鬼魂每一時間段都會分裂,佔據所有距離他兩個單位長度以內的格子。M或者G碰到鬼魂的話就會死去。

很明顯的雙向BFS,M和G同時出發。有一方走到對面走過的點上即為成功相遇。

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define M(a,b) memset(a,b,sizeof(a))
const int MAXN = 805;
const int INF = 0x3f3f3f3f;
int X[4] = {0,0,-1,1};
int Y[4] = {-1,1,0,0};
int vis[2][MAXN][MAXN];///記錄兩者點的訪問情況
int n,m,ans;
char MAP[MAXN][MAXN];
struct Node
{
    int x,y;
} z[2],ng,nm;
queue<Node>q[2];///雙向bfs,兩個對列。
void init()
{
    M(vis,0);
    while(!q[1].empty()) q[1].pop();
    while(!q[0].empty()) q[0].pop();
}
int bfs(int s)
{
    int len = q[s].size();

    while(len--)
    {
        Node temp = q[s].front();
        q[s].pop();
        int x1 = temp.x;
        int y1 = temp.y;
        /*
        
        如果走到某一點
        該點距離某一個鬼魂的距離小於步數*2的話
        則該點無法行走
        abs(z[0].x-x1)+abs(z[0].y-y1)>ans*2
        abs(z[1].x-x1)+abs(z[1].y-y1)>ans*2
        */
        if(x1>=0&&x1<n&&y1>=0&&y1<m&&MAP[x1][y1]!='X'&&abs(z[0].x-x1)+abs(z[0].y-y1)>ans*2&&abs(z[1].x-x1)+abs(z[1].y-y1)>ans*2)///剛開始要判斷一次
        {
        }
        else
        {
            continue;
        }
        for(int i=0;i<4;i++)
        {
            int xx = x1+X[i];
            int yy = y1+Y[i];
            if(xx>=0&&xx<n&&yy>=0&&yy<m&&MAP[xx][yy]!='X'&&abs(z[0].x-xx)+abs(z[0].y-yy)>ans*2&&abs(z[1].x-xx)+abs(z[1].y-yy)>ans*2)
            {
                if(vis[s][xx][yy]==0)///走到某個點
                {
                    if(vis[1-s][xx][yy]==1)///對方走過這個點
                    {
                        return 1;///符合條件
                    }
                    vis[s][xx][yy] = 1;
                    q[s].push({xx,yy});
                }
            }
        }
    }
    return 0;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        init();
        scanf("%d%d",&n,&m);
        for(int i=0; i<n; i++)
        {
            scanf("%s",MAP[i]);
        }
        int temp = 0;
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                if(MAP[i][j] == 'G')
                {
                    ng = {i,j};
                    vis[1][i][j] = 1;
                    q[1].push(ng);
                }
                else if(MAP[i][j] == 'M')
                {
                    nm = {i,j};
                    vis[0][i][j] = 1;
                    q[0].push(nm);
                }
                else if(MAP[i][j] == 'Z')
                {
                    z[temp++]  = {i,j};
                }
            }
        }
        ans = 0;
        int flag  = 0;
        while(!q[0].empty()||!q[1].empty())
        {
            ans++;
            if(bfs(0)==1)///M走三步
            {
                flag  = 1;
                break;
            }
            if(bfs(0)==1)
            {

                flag  = 1;
                break;
            }
            if(bfs(0)==1)
            {

                flag  = 1;
                break;
            }
            if(bfs(1)==1)///G走一步
            {
                flag = 1;
                break;
            }
        }
        if(flag==0)
        {
            printf("-1\n");
        }
        else
        {
            printf("%d\n",ans);
        }
    }
    return 0;
}