码迷,mamicode.com
首页 > Web开发 > 详细

HDU 3085 Nightmare Ⅱ【BFS +曼哈顿距离+综合性较强】

时间:2016-08-01 15:55:57      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:

Nightmare Ⅱ

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 16   Accepted Submission(s) : 6

Font: Times New Roman | Verdana | Georgia

Font Size:  

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


#include<stdio.h>
#include<queue>
#include<math.h>
#include<string.h>
#include<stdlib.h>
using namespace std;
char map[810][810];
int step[4][2]= {1,0,-1,0,0,1,0,-1};
struct node
{
    int x,y;
} M,G,Z[2];
int num_of_step,n,m;
queue<node> q[3];

bool ghost(node a)
{
    for(int i=0; i<2; i++)
    {
        if(abs(a.x-Z[i].x)+abs(a.y-Z[i].y) <= 2*num_of_step)//
            return false;//被抓
    }
    return true;
}

bool check(node b)
{
    if(b.x>=0&&b.y>=0&&b.x<n&&b.y<m&&map[b.x][b.y]!='X')
        return true;
    return false;
}

bool BFS(int people,int time,int start,int endd)
{
    node cur,next;
    q[2]=q[people];

    for(int i=0; i<time; i++)
    {
        while(!q[2].empty())
        {
            cur=q[2].front();
            q[2].pop();
            q[people].pop();
            if(ghost(cur))  //如果被抓
            {
                for(int i=0; i<4; i++)
                {
                    next=cur;
                    next.x+=step[i][0];
                    next.y+=step[i][1];
                    if(ghost(next)&&check(next)&&map[next.x][next.y]!=start)
                    {
                        if(map[next.x][next.y]==endd)
                            return true;
                        map[next.x][next.y]=start;
                        q[people].push(next);
                    }
                }
            }
        }
        q[2]=q[people];
    }
    return false;
}

int solve ()
{

    for(int i=0; i<3; i++) ////clear
    {
        while(!q[i].empty())
            q[i].pop();
    }
    num_of_step=0;
    q[0].push(M);
    q[1].push(G);
    while(!q[0].empty()&&!q[1].empty())////
    {
        num_of_step++;
        if(BFS(0,3,'M','G')||BFS(1,1,'G','M'))////
            return num_of_step;
    }
    return -1;
}

int main (void)
{
    int N;
    scanf("%d",&N);
    while(N--)
    {
        int cnt=0;
        scanf("%d%d",&n,&m);
        //getchar();
        for(int i=0; i<n; i++)
        {
            scanf("%s",map[i]);
            for(int ii=0; ii<m; ii++)
            {
                if(map[i][ii]=='M')      M.x=i,M.y=ii;
                else if(map[i][ii]=='G') G.x=i,G.y=ii;
                else if(map[i][ii]=='Z') Z[cnt].x=i,Z[cnt].y=ii,++cnt;
            }
        }
        //getchar();
        printf("%d\n",solve());
    }
    return 0;
}


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

HDU 3085 Nightmare Ⅱ【BFS +曼哈顿距离+综合性较强】

标签:

原文地址:http://blog.csdn.net/qq_24653023/article/details/52085963

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!