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

HDU 2nd “Vegetable-Birds Cup” Programming Open Contest

 

 

Recommend

lcy

 

题意:

M在一个迷宫中去找G

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

M或G只要走到对方走到过的点上即算相遇。(意思是其中一个人可以原地不动)

迷宫中有很多鬼魂,鬼魂每一时间段都会分裂,占据所有距离他两个单位长度以内的格子。M或者G碰到鬼魂的话就会死去。

 

 

很明显的双向BFS,M和G同时出发。有一方走到对面走过的点上即为成功相遇。

 

#include
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;
queueq[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=0&&y1ans*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=0&&yyans*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

 

你可能感兴趣的:(HDU题解,搜索)