Nightmare Ⅱ HDU - 3085(双向BFS)

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

这题比较有意思的一点就是,每个人的扩展层数是不一样的,假设如果每个人的扩展层数都是1,一般的实现方法都是可以用一个队列来实现的。但是不是这种情况的,就用两个队列来实现了,但是又为了实现能连续拓展3层,在执行时,实现记录队列的个数,然后就只有pop这个次数就好(因为还会入队一下元素),这样就可以满足逐层扩展了。
代码:输入格式有点问题,因为对bufferedReader的使用还不是特别熟练,但是算法应该没有问题。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main 
{
    static int b[][]= {{1,0},{-1,0},{0,1},{0,-1}};
    static char map[][]=new char[810][810];
    static boolean vis[][][]=new boolean[2][810][810];
    static int n,m,step;
    static node zz[]=new node[2];
    static node G,M;
    static boolean check(int x,int y)
    {
        if(x<1||x>n||y<1||y>m)
            return false;
        for(int i=0;i<2;i++)
        {
            if(Math.abs(x-zz[i].x)+Math.abs(y-zz[i].y)<=2*step||map[x][y]=='X')
                return false;
        }
        return false;
    }
    static Queue que[]=new LinkedList[2];
    static boolean bfs(int w)
    {
        node cur;
        int sum=que[w].size();
        while(sum-->0)
        {
            cur=que[w].poll();
            if(!check(cur.x,cur.y))continue;
            for(int i=0;i<4;i++)
            {
                int xx=cur.x+b[i][0];
                int yy=cur.y+b[i][1];
                if(!check(xx,yy))continue;
                if(!vis[w][xx][yy])
                {
                    if(vis[w^1][xx][yy])return true;
                    vis[w][xx][yy]=true;
                    que[w].add(new node(xx,yy));
                }
            }
        }
        return false;
    }
    static int work()
    {
        while(!que[0].isEmpty())que[0].poll();
        while(!que[1].isEmpty())que[1].poll();
        que[0].add(M);
        que[1].add(G);
        for(int i=0;i<2;i++)
            for(int j=1;j<=n;j++)
                Arrays.fill(vis[i][j],false);
        vis[0][M.x][M.y]=vis[1][G.x][G.y]=true;
        step=0;
        while((!que[0].isEmpty())||(!que[1].isEmpty()))
        {
            step++;
            for(int i=0;i<3;i++)
                if(bfs(0))return step;
            if(bfs(1))
                return step;
        }
        return -1;
    }
    public static void main(String[] args) throws IOException
    {
        que[0]=new LinkedList();
        que[1]=new LinkedList();
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();

        //System.out.println("haha");
        br.read();
        while(t-->0)
        {
            n=sc.nextInt();
            //System.out.println(n);
            //br.read();
            m=sc.nextInt();
            int cnt=0;
            br.read();
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=m;j++)
                {
                    map[i][j]=(char)br.read();
                    if(map[i][j]=='Z')
                        zz[cnt++]=new node(i,j);
                    else
                        if(map[i][j]=='M')
                            M=new node(i,j);
                        else
                            if(map[i][j]=='G')
                                G=new node(i,j);
                }
                br.read();
            }
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=m;j++)
                    System.out.print(map[i][j]);
                System.out.println();
            }
            System.out.println(work());

        }

    }
}
class node
{
    int x,y;
    node(int _x,int _y)
    {
        x=_x;
        y=_y;
    }
    node(){}
}

你可能感兴趣的:(双向BFS)