hdu3085Nightmare Ⅱ(双向广搜)

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
HDU 2nd “Vegetable-Birds Cup” Programming Open Contest
Statistic | Submit | Back
Close | Top
Hangzhou Dianzi University Online Judge
Copyright © 2005-2016 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 3, Now is : 2016-07-23 01:53:18, Gzip enabled, Version : 3.0
Administration
题意:n*m地图上有
‘. ’:路
‘X’:墙
’Z’:鬼,每秒蔓延2个单位长度,可以穿墙,共两个,每秒开始时鬼先动
‘M’:一号,每分钟可移动3个单位长度
‘G’:二号,每分钟课移动1个单位长度
问两人是否可以成功碰面,再不被鬼吃掉的前提下,鬼是先走;
思路:
1 用双向广搜搜索两个人同时出发经过的路径;
2 因为M是可以走三步的,我们可以在M行动时一个单位时间内利用三次BFS;
3 因为鬼是可以穿墙的,而且鬼先走,我们不必记录鬼走的路径,只需记录两个人在t时刻的点不考虑墙离鬼位置的最短距离s>t*2,乘以二是因为鬼一个单位时间走两步;
注意点:刚开始做出来的时候一直WA,因为是有多个测试组的,在输出一个数据组时忘记把队列清空,影响到后面的测试;
代码:

#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
struct node{
   int x,y;
}z[2];
queue<node>d[2];
int m_x,m_y,g_x,g_y;
int n,m,raod;
int vis[4][2]={0,1,0,-1,1,0,-1,0};
char map[850][850];
int mark[2][850][850];
int ok(int x,int y)
{
    int zx=abs(x-z[0].x)+abs(y-z[0].y);
    int zy=abs(x-z[1].x)+abs(y-z[1].y);
    if(x>=0&&x<n&&y>=0&&y<m&&map[x][y]!='X'&&zx>2*raod&&zy>2*raod)
        return 1;
    else
        return 0;
}
int bfs(int t)
{
    node q,p;
    int sum=d[t].size();
    while(sum--)  //这里只需要将t时刻的点再走一步;
    {
        q=d[t].front();
        d[t].pop();
        if(!ok(q.x,q.y))
            continue;
        for(int i=0;i<=3;i++)
        {
            p.x=q.x+vis[i][0];
            p.y=q.y+vis[i][1];
            if(ok(p.x,p.y)&&!mark[t][p.x][p.y])
            {
                if(mark[t^1][p.x][p.y])
                {
                    return 1;
                }
                mark[t][p.x][p.y]=1;
                d[t].push(p);
            }
        }
    }
    return 0;
}
int solve()
{
    while (!d[0].empty()) d[0].pop();
    while (!d[1].empty()) d[1].pop();//这里记得清空,防止之前的数据影响后面;
    memset(mark,0,sizeof(mark));
    mark[0][m_x][m_y]=1;
    mark[1][g_x][g_y]=1;
    node q,p;
    q.x=m_x,q.y=m_y;
    d[0].push(q);
    q.x=g_x,q.y=g_y;
    d[1].push(q);
    raod=0;
    while(d[0].size()||d[1].size())
    {
        raod++;
        if(bfs(0)==1)  return raod;
        if(bfs(0)==1)  return raod;
        if(bfs(0)==1)  return raod;//因为M走三步,所以三次BFS
        if(bfs(1)==1)  return raod;
    }
    return -1;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
            scanf("%s",map[i]);
        int tt=0;
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
            {
                if(map[i][j]=='M')
                    m_x=i,m_y=j;
                if(map[i][j]=='G')
                    g_x=i,g_y=j;
                if(map[i][j]=='Z')
                    z[tt].x=i,z[tt++].y=j;
            }
        printf("%d\n",solve());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值