[kuangbin带你飞]搜索进阶 G - Nightmare Ⅱ


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. 
InputThe 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. 
OutputOutput 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

题目大意:

        输入n*m的字符矩阵,矩阵中的M速度是3格/m,G的速度是1格/m,Z是鬼,初始有两个,每秒可以变出很多的分身Z(变出的分身在下一秒钟仍然可以变出无数个分身),占领跟Z距离是2的方格,直到占领所有的方格,每次都是鬼先占领方格,然后是M跟G走,M跟G可以同时都走,也可以有一个在原地不动,一个在走
        一般先想到的是预处理Z,求出Z占领方格ij时的时间,但是,在这一题没必要,MG可以边走边判断,假设M/G走到方格str[i][j]时耗时为t,距离两个原始鬼魂的最短距离是dis,如果dis         还有一点就是什么时候MG走or不走,想要输出的时间最少,那必定是两个人尽可能的多的走路,唯一的一个区别就是最后一步,如果他们最后的距离就只有3,那只要M走就可以了,如果他们最后的距离是1,那只要G走就可以了


#include
#include
#include
#include
#include
#include
using namespace std;
typedef pair ii;
#define x first
#define y second
char nm[810][810];
int n,m,s;
int mo[4][2] = {1,0,-1,0,0,1,0,-1};
ii G,M,Z[2];
queue q[2];
bool judge(ii t)    //判断是否被鬼抓到
{
    if(t.x<1 || t.x>n || t.y<1 || t.y>m)    return 0;
    int l1,l2;
    l1 = abs(t.x-Z[0].x)+abs(t.y-Z[0].y);
    l2 = abs(t.x-Z[1].x)+abs(t.y-Z[1].y);
    if(l1<=2*s || l2<=2*s || nm[t.x][t.y]=='X' || nm[t.x][t.y]=='\0')
        return 0;
    return 1;
}
bool bfs(int bj,int t,char h,char e)
{
    while(t--)
    {
        queue qt = q[bj];   //qt为临时队列,拷贝在该时刻所有q[bj]中的状态
        while(!qt.empty())
        {
            ii no = qt.front();
            qt.pop();q[bj].pop();
            if(!judge(no))  continue;
            int i;
            for(i=0;i<4;i++)
            {
                ii ne(no.x+mo[i][0],no.y+mo[i][1]);
                if(!judge(ne) || nm[ne.x][ne.y]==h) continue;
                if(nm[ne.x][ne.y] == e)   return 1;
                nm[ne.x][ne.y] = h;
                q[bj].push(ne);
            }
        }
    }
    return 0;
}
int solve()
{
    s = 0;
    // q[0]代表bfs M的队列,q[1]代表bfs G的队列
    while(!q[0].empty()) q[0].pop();
    while(!q[1].empty()) q[1].pop();
    q[0].push(M);
    q[1].push(G);
    while(!q[0].empty() && !q[1].empty())
    {
        s++;
        bool bj1 = bfs(0,3,'M','G');
        bool bj2 = bfs(1,1,'G','M');
        if(bj1 || bj2)  return s;
    }
    return -1;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        memset(nm,'X',sizeof(nm));
        int i,j,k=0;
        for(i=1;i<=n;i++)
        {
            scanf("%s",nm[i]+1);
            for(j=1;j<=m;j++)
            {
                if(nm[i][j] == 'M') M.x=i,M.y=j;
                if(nm[i][j] == 'G') G.x=i,G.y=j;
                if(nm[i][j] == 'Z') Z[k].x=i,Z[k++].y=j;
            }
        }
        printf("%d\n",solve());
    }
    return 0;
}


你可能感兴趣的:(搜索)