HDU 3085 Nightmare Ⅱ

Nightmare Ⅱ

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


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
 
思路:双向广搜
 
代码:
#include <iostream>
#include <cstring>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <cmath>
#include <algorithm>
using namespace std;
int t,n,m;
char map[810][810];
int the_woman_x,the_woman_y;
int the_man_x,the_man_y;
int ghost_one[2],ghost_two[2];
int visit_woman[810][810],visit_man[810][810];
int the_last_step;
int the_last_flag;
int move[4][2] = {1,0,-1,0,0,1,0,-1};
struct Node
{
    int x,y;
}q_one[100000],q_two[100000];
bool isghost(int x,int y,int step)
{
    if(x < 0 || x >= n || y < 0 || y >= m || map[x][y] == 'X')
       return false;
    int g_one = 0,g_two = 0;
    g_one = (abs(x - ghost_one[0]) + abs(y - ghost_one[1]));
    g_two = (abs(x - ghost_two[0]) + abs(y - ghost_two[1]));
    if(step * 2 >= g_one || step * 2 >= g_two)
        return false;
    return true;
}
void BFS()
{
    int head_man = 0,head_woman = 0,tail_man = -1,tail_woman = -1;
    int step = 1;
    Node top;top.x = the_man_x;top.y = the_man_y;
    q_one[++ tail_man] = top;
    Node cur;cur.x = the_woman_x;cur.y = the_woman_y;
    q_two[++ tail_woman] = cur;
    while(1)
    {
        int flag = 0;
        for(int i = 1;i <= 3;i ++)
        {
            int temp = tail_man;
            while(head_man <= temp)
            {
                flag = 1;
                Node now;
                now = q_one[head_man];
                head_man ++;
                int x = now.x;int y = now.y;
                if(!isghost(x,y,step)) continue ;
                for(int j = 0;j < 4;j ++)
                {
                    int nowx = now.x + move[j][0];
                    int nowy = now.y + move[j][1];
                    if(!isghost(nowx,nowy,step) || visit_man[nowx][nowy] == 1)
                        continue ;
                    if(visit_woman[nowx][nowy] == 1)
                    {
                        the_last_step = step;
                        return ;
                    }
                    visit_man[nowx][nowy] = 1;
                    Node man;man.x = nowx;man.y = nowy;
                    q_one[++ tail_man] = man;
                }

            }
        }
        int temp = tail_woman;
        while(head_woman <= temp)
        {
            flag = 1;
            Node now = q_two[head_woman];
            head_woman ++;
            int x = now.x;int y = now.y;
            if(!isghost(x,y,step)) continue ;
            for(int j = 0;j < 4;j ++)
            {
                int nowx = now.x + move[j][0];
                int nowy = now.y + move[j][1];
                if(!isghost(nowx,nowy,step) || visit_woman[nowx][nowy] == 1)
                    continue ;
                if(visit_man[nowx][nowy] == 1)
                {
                    the_last_step = step;
                    return ;
                }
                visit_woman[nowx][nowy] = 1;
                Node woman;woman.x = nowx;woman.y = nowy;
                q_two[++ tail_woman] = woman;
            }
        }
        step ++;
        if(flag == 0)
           break ;
    }
    return ;
}
int main()
{
    scanf("%d",&t);
    while(t --)
    {
        scanf("%d%d",&n,&m);
        memset(map,0,sizeof(map));
        memset(ghost_one,0,sizeof(ghost_one));
        memset(ghost_two,0,sizeof(ghost_two));
        memset(visit_woman,0,sizeof(visit_woman));
        memset(visit_man,0,sizeof(visit_man));
        memset(q_one,0,sizeof(q_one));
        memset(q_two,0,sizeof(q_two));
        int flag = 0;
        for(int i = 0;i < n;i ++)
        {
            scanf("%s",map[i]);
            for(int j = 0;j < m;j ++)
            {
                if(map[i][j] == 'Z')
                {
                    if(flag == 0)
                    {
                        ghost_one[0] = i;ghost_one[1] = j;
                        flag ++;
                    }
                    else
                    {
                        ghost_two[0] = i;ghost_two[1] = j;
                    }

                }
                if(map[i][j] == 'M')
                {
                    the_man_x = i;the_man_y = j;
                    visit_man[i][j] = 1;
                }
                if(map[i][j] == 'G')
                {
                    the_woman_x = i;the_woman_y = j;
                    visit_woman[i][j] = 1;
                }
            }
        }
        the_last_step = 0;
        BFS();
        if(the_last_step == 0)
            printf("-1\n");
        else
            printf("%d\n",the_last_step);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/GODLIKEING/p/3356406.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值