HDU 3085 Nightmare Ⅱ (双向bfs+曼哈顿距离运用)

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

题目大意:

男主和女友到了迷宫,但是走散了,有两只鬼会杀死他们,他们想见一面,如果可以输出最小时间。。吐槽一下,出口都没不是必死么qaq 

解题思路:

这题做了我一下午,一开始没想到用曼哈顿距离,开了三个队列,把鬼也一起bfs,结果毫无疑问的tle了,后来看了别人的题解才知道用曼哈顿距离,不过用了以后因为代码比较长各种粗心耗掉了一下午。。我还是太弱了,wa了n次,ac的一瞬间要感动哭了,或许这就是acm的乐趣吧

代码:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<math.h>
#include<string>
#include<stdio.h>
#include<queue>
#include<stack>
#include<map>
using namespace std;
struct node
{
    int x,y,t,d;
};
int p1[805][805];
int p2[805][805];
char s[805][805];
int gx[2];
int gy[2];
int n,m,mstx,msty,wstx,wsty;
int dirx[4]={0,0,-1,1};
int diry[4]={1,-1,0,0};
queue<node>q1;
queue<node>q2;
int main()
{
    int i,j,k,test,tag,flag;
    node now,next;
    scanf("%d",&test);
    while(test--)
    {
        int d=0;
        scanf("%d%d",&m,&n);
        for(i=0;i<m;i++)
        {
            char s1[805];
            scanf("%s",s1);
            for(j=0;j<n;j++)
            {
                p1[i][j]=0;
                p2[i][j]=0;
                s[i][j]=s1[j];
                if(s[i][j]=='G')
                {
                    wstx=i;
                    wsty=j;
                }
                else if(s[i][j]=='M')
                {
                    mstx=i;
                    msty=j;
                }
                else if(s[i][j]=='Z')
                {
                    gx[d]=i;
                    gy[d]=j;
                    d++;
                }
            }
        }
        if(abs(mstx-gx[0])+abs(msty-gy[0])<=2||abs(mstx-gx[1])+abs(msty-gy[1])<=2||abs(wstx-gx[0])+abs(wsty-gy[0])<=2||abs(wstx-gx[1])+abs(wsty-gy[1])<=2)
        {
            printf("-1\n");
            continue;
        }
        while(!q1.empty())q1.pop();
        while(!q2.empty())q2.pop();
        now.t=0;
        now.x=mstx;
        now.y=msty;
        now.d=0;
        p1[now.x][now.y]=1;
        q1.push(now);
        now.x=wstx;
        now.y=wsty;
        p2[now.x][now.y]=1;
        q2.push(now);
        tag=0;
        flag=0;
        int xx,yy,t;
        while(!q1.empty()&&!q2.empty())
        {
            now=q2.front();
            t=now.t+1;
            while(now.t!=t&&!q2.empty())
            {
                int o=1;
                if(abs(now.x-gx[0])+abs(now.y-gy[0])<=t*2||abs(now.x-gx[1])+abs(now.y-gy[1])<=t*2)
                    o=0;
                if(o)
                for(i=0;i<4;i++)
                {
                    xx=now.x+dirx[i];
                    yy=now.y+diry[i];
                    if(xx>=0&&yy>=0&&xx<m&&yy<n&&s[xx][yy]!='X'&&!p2[xx][yy]&&abs(xx-gx[0])+abs(yy-gy[0])>t*2&&abs(xx-gx[1])+abs(yy-gy[1])>t*2)
                    {
                        //printf("girl:x=%d  y=%d!!\n",xx,yy);
                        if(p1[xx][yy])
                        {
                            tag=1;
                            break;
                        }
                        next.x=xx;
                        next.y=yy;
                        next.t=now.t+1;
                        p2[xx][yy]=1;
                        q2.push(next);
                    }
                }
                if(tag)
                    break;
                q2.pop();
                if(!q2.empty())
                now=q2.front();
                else
                {
                    break;
                }
            }
            if(tag)
                break;
            now=q1.front();
            while(now.t!=t)
            {
                int o=1;
                if(abs(now.x-gx[0])+abs(now.y-gy[0])<=t*2||abs(now.x-gx[1])+abs(now.y-gy[1])<=t*2)
                    o=0;
                if(o)
                for(i=0;i<4;i++)
                {
                    xx=now.x+dirx[i];
                    yy=now.y+diry[i];
                    if(xx>=0&&yy>=0&&xx<m&&yy<n&&s[xx][yy]!='X'&&!p1[xx][yy]&&abs(xx-gx[0])+abs(yy-gy[0])>t*2&&abs(xx-gx[1])+abs(yy-gy[1])>t*2)
                    {
                        //printf("boy:x=%d  y=%d!!\n",xx,yy);
                        if(p2[xx][yy])
                        {
                            tag=1;
                            break;
                        }
                        next.x=xx;
                        next.y=yy;
                        next.d=now.d+1;
                        next.t=next.d/3;
                        p1[xx][yy]=1;
                        q1.push(next);
                    }
                }
                if(tag)
                    break;
                q1.pop();
                if(!q1.empty())
                now=q1.front();
                else
                {
                    break;
                }
            }
            if(tag)
                break;
        }
        if(tag)
        {
            printf("%d\n",t);
        }
        else
            printf("-1\n");
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值