解题报告:HDU_3085 Nightmare Ⅱ BFS

Nightmare Ⅱ

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


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
 

Author
二日月
 

Source
 

Recommend
lcy   |   We have carefully selected several similar problems for you:   3083  3084  3086  3087  3079 
 

Statistic |  Submit |  Discuss | Note



题意:
一个二维迷宫中有两只鬼,他们以每秒两格的单位扩展(无视地形),你和你的py朋友在里面,你一秒可以走3格,你的py朋友一秒只能走1格,问能不能在鬼kill你们前相遇。每秒开始前鬼先动。

思路:
双向BFS,有一些细节要注意:
1. 有的格子是可以进但是不能出的(因为下一秒鬼先动,然后就kill你了),所以从队列里面取出时要判断一下。
2. 一秒走n步不意味着可以一秒可以到n格的距离,因为可能第1步都走不出去(已经被鬼占领了),那后面的步子肯定是走不出来了。


代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>

const int X[4] = {-1,0,0,1};
const int Y[4] = {0,-1,1,0};

using namespace std;

bool ok;
int n,m;
int z[2][2];
int mx,my;
int gx,gy;
char mp[805][805];
int erriyue[805][805];
bool vis[805][805];
#define fi first
#define se second
queue<pair<int,int> >Q;
queue<pair<int,int> > QQ;
inline int ghost(int& x,int& y){
    return (min(abs(x-z[0][0])+abs(y-z[0][1]),abs(x-z[1][0])+abs(y-z[1][1]))+1)/2;
}
bool vis2[805][805];
int girl[805][805];

inline bool check(int& a,int& b){
    for(int i=0;i<4;i++){
        int x = a + X[i];
        int y = b + Y[i];
        if(vis[x][y]){
            return true;
        }
    }return false ;
}

void bfs(){
    while(!Q.empty())Q.pop();
    for(int k=0;k<3;k++)
    while(!QQ.empty())QQ.pop();
    memset(erriyue,0x3f,sizeof(erriyue));
    memset(girl,0x3f,sizeof(girl));
    memset(vis,0,sizeof(vis));
    memset(vis2,0,sizeof(vis2));

    vis[mx][my]=true;vis2[gx][gy] = true;
    QQ.push(make_pair(mx,my));Q.push(make_pair(gx,gy));
    girl[gx][gy]=0;erriyue[mx][my]=0;



int all = -1;
while(1){all++;

if(Q.empty()&&QQ.empty())return ;

/******* man *********/
for(int k=1,x,y,step;k<=3;k++){
int sz = QQ.size();
while(sz--){
    x = QQ.front().fi;
    y = QQ.front().se;
    if(k==1){
        step = erriyue[x][y];
        if(step+1==ghost(x,y)){
            QQ.pop();
            continue;
        }
    }


    QQ.pop();

    for(int i=0;i<4;i++){
        int a = x + X[i];
        int b = y + Y[i];
        if(a<1||b<1||a>n||b>m||mp[a][b]=='X'||vis[a][b]||ghost(a,b)<=step+1){
            continue ;
        }
        QQ.push(make_pair(a,b));
        vis[a][b] = true;
        erriyue[a][b] = step+1;

        if(vis2[a][b]){
            printf("%d\n",erriyue[a][b]);
            ok = true;
            return ;
        }

    }
}


}


/*******  girl *******/
    int sz = Q.size();
    while(sz--){
        int  x = Q.front().fi;
        int  y = Q.front().se;
        int  step = girl[x][y];
        if(step+1==ghost(x,y)){
            Q.pop();
            continue;
        }

        if(step!=all)break;
        Q.pop();

        for(int i=0;i<4;i++){
            int a = x + X[i];
            int b = y + Y[i];
            if(!a||!b||a>n||b>m||vis2[a][b]||mp[a][b]=='X'||ghost(a,b)<=step+1){
                continue;
            }
            Q.push(make_pair(a,b));
            vis2[a][b] = true;
            girl[a][b] = step+1;

            if( vis[a][b] ){
                printf("%d\n",girl[a][b]);
                ok = true;
                return ;
            }
        }
    }

}


}

int main()
{
    int T;scanf("%d",&T);
    while(T--){ ok = false ;
        scanf("%d%d",&n,&m);
        int k = 0;
        for(int i=1;i<=n;i++){
            scanf("%s",mp[i]+1);
            for(int j=1;j<=m;j++){
                if(mp[i][j]=='M'){
                    mx = i ; my = j;
                }else if(mp[i][j]=='G'){
                    gx = i; gy = j;
                }else if(mp[i][j]=='Z'){
                    z[k][0] = i;z[k][1] = j;
                    k++;
                }
            }
        }


        bfs();

        if(!ok){
            printf("-1\n");
        }

    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值