HDU3085 Nightmare Ⅱ[双向bfs]


D - Nightmare Ⅱ
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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
 

题意:

看M和G能不能在不遇到鬼的情况下,找到对方.

M一次可以走三步 G只能走一步 鬼可以走两步 



貌似用优先队列改了队列里面的顺序..不能用
改成队列就过了.

完全不知道为什么..不管了..

M的三步我用了一个leave去保存, 从0-2 到了2之后 就把该点的步数加一 放在队列里面

其余的 记得先比较跟鬼的距离,因为鬼是先走的,当前点如果和鬼的曼哈顿距离 小于2*step的话 , 就不能用该点 continue






#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
using namespace std;
const int N=810;
int n,m;
char Map[N][N];
bool mark[N][N][2],flag,used[N][N];
int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
struct node
{
    int x,y,step,leave;
}M,G,Z[2];
queue<node>q;
queue<node>p;
bool check(int x,int y,int num,int step)
{
    if (x<0 || x>=n || y<0 || y>=m || mark[x][y][num] || Map[x][y]=='X' || abs(x-Z[0].x)+abs(y-Z[0].y)<=2*step || abs(x-Z[1].x)+abs(y-Z[1].y)<=2*step)
        return 0;
    return 1;
}
bool check(int x,int y,int step)
{
    if (abs(x-Z[0].x)+abs(y-Z[0].y)<=2*step || abs(x-Z[1].x)+abs(y-Z[1].y)<=2*step)
        return 0;
    return 1;
}
int bfs()
{
    flag=0;
    while (!q.empty())
        q.pop();
    while (!p.empty())
        p.pop();
    q.push(M);
    p.push(G);
    int s=0;
    while (!q.empty() || !p.empty())
    {
        node start,later;
        while (!q.empty() && q.front().step<=s)
        {
            start=q.front();
            q.pop();
            if (!check(start.x,start.y,start.step+1))
                    continue;
            for (int i=0 ; i<4 ; i++)
            {
                int nx=start.x+dir[i][0];
                int ny=start.y+dir[i][1];
                if (check(nx,ny,0,start.step+1))
                {
                    if (mark[nx][ny][1])
                        return start.step+1;
                    mark[nx][ny][0]=1;
                    later.x=nx;
                    later.y=ny;
                    later.step=start.step;
                    if (start.leave==2)
                    {
                        later.leave=0;
                        later.step++;
                    }
                    else
                        later.leave=start.leave+1;
                    q.push(later);
                }
            }
        }
        while (!p.empty() && p.front().step<=s)
        {
            start=p.front();
            p.pop();
            if (!check(start.x,start.y,start.step+1))
                continue;
            for (int i=0 ; i<4 ; i++)
            {
                int nx=start.x+dir[i][0];
                int ny=start.y+dir[i][1];
                if (check(nx,ny,1,start.step+1))
                {
                    if (mark[nx][ny][0])
                        return start.step+1;
                    mark[nx][ny][1]=1;
                    later.x=nx;
                    later.y=ny;
                    later.step=start.step+1;
                    later.leave=start.leave;
                    p.push(later);
                }
            }
        }
        s++;
    }
    return -1;
}
int main()
{
    int T;
    scanf("%d",&T);
    while (T--)
    {
        scanf("%d%d",&n,&m);
        memset(mark,0,sizeof(mark));
        int z=0;
        for (int i=0 ; i<n ; i++)
        {
            scanf("%s",Map[i]);
            for (int j=0 ; j<m ; j++)
            {
                if (Map[i][j]=='M')
                {
                    M.x=i;
                    M.y=j;
                    M.leave=0;
                    mark[i][j][0]=1;
                    M.step=0;
                }
                else if (Map[i][j]=='G')
                {
                    G.x=i;
                    G.y=j;
                    G.leave=0;
                    mark[i][j][1]=1;
                    G.step=0;
                }
                else if (Map[i][j]=='Z')
                {
                    Z[z].x=i;
                    Z[z].y=j;
                    Z[z++].step=0;
                }
            }
        }
        printf("%d\n",bfs());
    }
    return 0;
}



/*
5 6
XXXXXX
XZZ..X
XXXXXX
M.....
..G...
5 6
XXXXXX
XZZ..X
XXXXXX
M.....
.XG...
5 6
XXXXXX
XZZ..X
XXXXXX
MG....
.X....

5 6
XXXXXX
XZZ..X
XXXXXX
M.X...
XX.XG.

*/


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值