Nightmare Ⅱ HDU - 3085

题目链接:点我


    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

题意:

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值