hdu3085 Nightmare Ⅱ

Nightmare Ⅱ

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


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. (1The 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

  思路:双向bfs,m和g两边同时搜,如果碰面了,此时的step就是2人相遇花费的最少时间.注意m每秒可以走3格,
g每秒可以走1格,z每秒分身到距离自己的曼哈顿距离(之所以是曼哈顿距离是因为鬼只能上下左右这4个方向走,而
不能斜着走)2格和1格的全部格子,将其占领。
 注意读入地图的时候用scanf,用cin会超时,因为scanf效率比cin高,在读入的数据较多时,差异就体现了.

这里bfs每次拓展一层

a(i1,,j1) b(i2,j2) 那么ab的曼哈顿距离位abs(i1-i2)+abs(j1-j2)

#include
#include
#include
#include
#include
using namespace std;
#define maxn 805
int v[2][maxn][maxn];//2维
int d[5][2]=
{
    0,0,
    -1,0,
    1,0,
    0,-1,
    0,1
};
char mymap[maxn][maxn];
int xm,ym,xg,yg,xz1,yz1,xz2,yz2,n,m,step;
queueq[2];
struct node
{
    int x,y;
};
int judge(int x,int y)
{
    if((abs(x-xz1)+abs(y-yz1))<=2*step||(abs(x-xz2)+abs(y-yz2))<=2*step)
        return 0;
    else
        return 1;
}
int bfs(int type)
{
    struct node now;
    int xx,yy;
    int t=q[type].size();
    while(t--)
    {
        now=q[type].front();
        q[type].pop();
        if(!judge(now.x,now.y))
            continue;
        for(int i=1;i<=4;i++)
        {
            xx=now.x+d[i][0];
            yy=now.y+d[i][1];
            if(xx>=n||xx<0||yy>=m||yy<0) continue;
            if(v[type][xx][yy]||mymap[xx][yy]=='X') continue;
            if(!judge(xx,yy)) continue;
            if(v[type^1][xx][yy]) return 1;
            v[type][xx][yy]=1;
            q[type].push((node){xx,yy});

        }

    }
    return 0;

}
int solve()
{
    step=0;
    memset(v,0,sizeof(v));
    while(!q[0].empty()) q[0].pop();
    while(!q[1].empty()) q[1].pop();
    v[0][xm][ym]=v[1][xg][yg]=1;
    q[0].push((node){xm,ym});
    q[1].push((node){xg,yg});
    while(!q[0].empty()||!q[1].empty())
    {
        step++;
        if(bfs(0)||bfs(0)||bfs(0)||bfs(1)) return step;
    }
    return -1;




}
int main()
{
    int t;
    int znum;
    scanf("%d",&t);
    while(t--)
    {
        znum=0;
        scanf("%d %d",&n,&m);
        for(int i=0;i


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