hdu3085双向bfs+曼哈顿距离判断

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

题目大意:
男盆友和女盆友同时困在一个迷宫内,男的一次可以走三步,女的一次走一步,迷宫内还有两个魔鬼并且还会分身,墙以及有魔鬼的地方不可以走,但是魔鬼可以走墙,并且分身后的魔鬼还可以继续分身,问两人是否可以相遇;可以就输出最短的时间;
思路:
1,由于要判断两人是否可以相遇,我们就用双向bfs,要做到从两个人同时开始搜索,则需要设置一个全局step记录步数,只要进行了搜索,次序随便,就改变时间,自然就可以看做是同时进行的了;
2,由于题目说,可以看做魔鬼先走然后人再走,所以我们先加上时间,在来搜索。对于魔鬼,它可以走墙所在的位置,这个条件很关键,这决定了我们是否可以用曼哈顿距离来判断魔鬼是否走到当前的位置,而不用再来模拟魔鬼的走向;
3,还有一点就是,上次搜索的时候符合条件的点,这一次搜索不一定就符合了,因为,在每一秒的行走过程中,我们看成是魔鬼先走,人然后再走;
4,至于怎么实现男的一秒走三步,女的一秒走一步,那就是函数的调用次数问题了。而且在每一次搜索的时候,我们是将队列中的所有点全部拿出来进行扩展,这样就可以保证队列中的点的询问时间是一致的,也就是它们都是在同一步扩展出来的;

#include<bits/stdc++.h>
using namespace std;
int n,m;
const int maxn=805;
char mp[maxn][maxn];
bool vis[2][maxn][maxn];
int step,mx,my,gx,gy;
int z[2][2],cnt;
int flag=0;
struct node
{
    int x,y;
    node(int a=0,int b=0)
    {
        x=a,y=b;
    }
};
queue<node> q[2];
int dx[]= {0,0,-1,1};
int dy[]= {1,-1,0,0};
bool check(int who,int x,int y)
{
    return x>=0&&x<n&&y>=0&&y<m&&abs(x-z[0][0])+abs(y-z[0][1])>step*2&&abs(x-z[1][0])+abs(y-z[1][1])>step*2&&mp[x][y]!='X';
}
int bfs(int who)
{
    int sz=q[who].size();
    while(sz--)
    {
        node tmp=q[who].front();
        q[who].pop();
        if(!check(who,tmp.x,tmp.y)) continue;
        for(int i=0; i<4; i++)
        {
            int xx=tmp.x+dx[i];
            int yy=tmp.y+dy[i];
            if(check(who,xx,yy)&&vis[who][xx][yy]==false)
            {
                if(vis[1-who][xx][yy])
                {
                    flag=1;
                    return 1;
                }
                vis[who][xx][yy]=true;
                q[who].push(node(xx,yy));
            }
        }
    }
    return 0;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        step=0,cnt=0,flag=0;
        memset(vis,false,sizeof vis);
        scanf("%d%d",&n,&m);
        for(int i=0; i<n; i++)
        {
            scanf("%s",mp[i]);
            for(int j=0; j<m; j++)
            {
                if(mp[i][j]=='M')
                    mx=i,my=j;
                if(mp[i][j]=='G')
                    gx=i,gy=j;
                if(mp[i][j]=='Z')
                    z[cnt][0]=i,z[cnt][1]=j,cnt++;
            }
        }

        while(!q[0].empty()) q[0].pop();
        while(!q[1].empty()) q[1].pop();
        q[0].push(node(mx,my));
        q[1].push(node(gx,gy));
        vis[0][mx][my]=vis[1][gx][gy]=1;
        while(!q[0].empty()||!q[1].empty())
        {
            step++;
            if(bfs(0)) break;
            if(bfs(0)) break;
            if(bfs(0)) break;
            if(bfs(1)) break;
        }
        if(flag)
            printf("%d\n",step);
        else
            puts("-1");
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值