hdu3081 双向广搜

Nightmare Ⅱ

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


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
HDU 2nd “Vegetable-Birds Cup” Programming Open Contest
 

Recommend
lcy
 
了解了一下双向BFS
这道题类似吧,模拟题目要求的先鬼,后人~~~
双向bfs貌似不能用STL队列,因为要分层搜索,就是你搜一层,我搜一层~~ 而如果用STL不能实现。
思路来着别人~~
#include <iostream>
#include <cstring>
#include <cstdio>
#define N 805
#define INF 0x3f3f3f3f
#define BUG printf("here!\n")

using namespace std;
char map[805][805];
int mx,my,gx,gy,k;
int xx[4]={0,1,0,-1};
int yy[4]={1,0,-1,0};
struct node
{
    int x,y;
};
node zz[805];
int n,m;
node q1[N*N],q2[N*N],q3[N*N];
int bfs()
{
    int front1=0,front2=0,front3=0;
    int tail1=1,tail2=1,tail3=k;
    q1[0].x=mx,q1[0].y=my,q2[0].x=gx,q2[0].y=gy;
    int t=0;
    while(front1<tail1||front2<tail2)
    {
        int i,j;
        t++;
        int tmp;
        for(i=0;i<2;i++)
        {
            tmp=tail3;
            for(;front3<tmp;front3++)
            {
                int x=q3[front3].x;
                int y=q3[front3].y;
                for(j=0;j<4;j++)
                {
                    int nx=x+xx[j];
                    int ny=y+yy[j];
                    if(nx<0||nx>=n||ny<0||ny>=m)
                        continue;
                    if(map[nx][ny]!='Z')
                    {
                        map[nx][ny]='Z';
                        q3[tail3].x=nx;
                        q3[tail3++].y=ny;
                    }
                }
            }
        }
        tmp=tail2;
        for(;front2<tmp;front2++)
        {
            int x=q2[front2].x;
            int y=q2[front2].y;
            if(map[x][y]=='Z')
                continue;
            for(j=0;j<4;j++)
            {
                int nx=x+xx[j];
                int ny=y+yy[j];
                if(nx<0||nx>=n||ny<0||ny>=m)
                    continue;
                if(map[nx][ny]=='X'||map[nx][ny]=='Z'||map[nx][ny]=='G')
                    continue;
                if(map[nx][ny]=='.')
                {
                    map[nx][ny]='G';
                    q2[tail2].x=nx;
                    q2[tail2++].y=ny;
                }
                else
                {
                    return t;
                }


            }
        }
        for(i=0;i<3;i++)
        {
            tmp=tail1;
            for(;front1<tmp;front1++)
            {
                int x=q1[front1].x;
                int y=q1[front1].y;
                if(map[x][y]=='Z')
                    continue;
                for(j=0;j<4;j++)
                {
                    int nx=x+xx[j];
                    int ny=y+yy[j];
                    if(nx<0||nx>=n||ny<0||ny>=m)
                        continue;
                    if(map[nx][ny]=='X'||map[nx][ny]=='Z'||map[nx][ny]=='M')
                        continue;
                    if(map[nx][ny]=='.')
                    {
                        map[nx][ny]='M';
                        q1[tail1].x=nx;
                        q1[tail1++].y=ny;
                    }
                    else
                    {
                        return t;
                    }
                }
            }
        }
    }
    return -1;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {

        int i;
        k=0;
        scanf("%d%d",&n,&m);
        for(i=0;i<n;i++)
        {
            scanf("%s",map[i]);
            int j;
            for(j=0;j<m;j++)
            {
                if(map[i][j]=='M')
                    mx=i,my=j;
                else if(map[i][j]=='G')
                    gx=i,gy=j;
                else if(map[i][j]=='Z')
                    q3[k].x=i,q3[k++].y=j;
            }
        }
        int res=bfs();
        printf("%d\n",res);
    }
    return 0;
}



你可能感兴趣的:(hdu3081 双向广搜)