Nightmare Ⅱ HDU

来源:互联网 发布:淘宝公司招聘 编辑:程序博客网 时间:2024/05/19 02:30

题目链接:点我


    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

题意:

bfs,每次对两个人进行bfs,判断另一个人是否已经经过了这个点.

代码:

#include<cstring>#include<cstdio>#include<algorithm>#include<cmath>#include<iostream>#include<queue>using namespace std;char w[802][802];struct ss{    int x,y;}m,g,z[3];queue<ss> q[2];int n,mm,step;bool used[2][802][802];int dx[]={0,0,1,-1};int dy[]={1,-1,0,0};bool judge(int x,int y){    if(x>=0&&y>=0&&x<n&&y<mm)        if(abs(x-z[0].x)+abs(y-z[0].y)>2*step)            if(abs(x-z[1].x)+abs(y-z[1].y)>2*step)                if(w[x][y]!='X')            return true;    return false;}bool bfs(int w){    int sum=q[w].size();    while(sum--){//从当前已经在队列的每个元素进行扩展        ss p=q[w].front();        q[w].pop();        if(!judge(p.x,p.y)) continue;        for(int i=0;i<4;++i){            int x=p.x+dx[i];            int y=p.y+dy[i];            if(judge(x,y)){                if(used[w][x][y]==false){                    ss cur;                    cur.x=x;                    cur.y=y;                    q[w].push(cur);                    used[w][x][y]=true;                    if(used[w^1][x][y]==true)//判断是否另一个人走过                        return true;                }            }        }    }    return false;}int solve(){    while(!q[0].empty()) q[0].pop();    while(!q[1].empty()) q[1].pop();    step=0;    memset(used,false,sizeof(used));    q[0].push(m);    q[1].push(g);    used[0][m.x][m.y]=used[1][g.x][g.y]=true;    while(!q[0].empty()||!q[1].empty()){        step++;        for(int i=3;i>0;--i)//每搜一次代表走一步            if(bfs(0)) return step;        if(bfs(1)) return step;    }    return -1;}int main(){    int t;    scanf("%d",&t);    while(t--){        scanf("%d %d",&n,&mm);        for(int i=0;i<n;++i)            scanf("%s",w[i]);        int k=0;        for(int i=0;i<n;++i)            for(int j=0;j<mm;++j){            if(w[i][j]=='M')                m.x=i,m.y=j;            if(w[i][j]=='G')                g.x=i,g.y=j;            if(w[i][j]=='Z')                z[k].x=i,z[k++].y=j;        }        printf("%d\n",solve());    }    return 0;}
原创粉丝点击