HDU - 3085 Nightmare Ⅱ 搜索

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

题意:

erriye 梦见女友被困在迷宫里了,现在 erriye 需要去解救他的的女友

给出他女友和他的位置

迷宫里有两个ghost,每秒钟会分生出多个ghost占据在他2步之内的所有格子

little erriye 每秒可以移动3步

grilfriend每秒可以移动一步

ghost、erriye、grilfriend都只能上下左右移动(且人不能穿透墙壁,鬼可以)

人一旦与鬼魂占据同一格子,则被杀死

并且每次都是鬼先走,人后走

问:little erriye 最少需要多久才能解救其女友?,如果解救失败,输出-1

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
int n,m,step,flag;
int t[4][2]= {1,0,-1,0,0,1,0,-1};
char v[810][810];
char a[810][810];
struct p
{
    int x;
    int y;
} mm,gg;
queue<p>Q[2];
struct node
{
    int xx,yy;
} k[2];
int judge(p f)
{
    for(int i=0; i<2; i++)
        if(abs(f.x-k[i].xx)+abs(f.y-k[i].yy)<=2*step) //鬼是否可以到达这个点
            return 0;
    return 1;
}
int fun(int ds,int num,char s1,char s2)
{
    p now,tmp;
    queue<p>q;
    q=Q[ds];
    for(int j=0; j<num; j++)
    {
        while(!q.empty())
        {
            now=q.front();
            Q[ds].pop();
            q.pop();
            if(judge(now)) //鬼先走,所以要先判断再走
            {
                for(int i=0; i<4; i++)
                {
                    tmp.x=now.x+t[i][0];
                    tmp.y=now.y+t[i][1];
                    if(judge(tmp)==0) //判断现在走到的点鬼是否已经到达
                        continue;
                    if(tmp.x<0||tmp.x>=n||tmp.y<0||tmp.y>=m||a[tmp.x][tmp.y]=='X'||a[tmp.x][tmp.y]=='Z') //出界或到墙的位置或到鬼的位置
                        continue;
                    if(v[tmp.x][tmp.y]==s1)//曾经已经走到过
                        continue;
                    if(v[tmp.x][tmp.y]==s2) //走到了对方曾到过的点
                        return 1;
                    v[tmp.x][tmp.y]=s1; //标记为走过
                    Q[ds].push(tmp);
                }
            }
        }
        q=Q[ds];
    }
    return 0;
}
void bfs()
{
    while(!Q[0].empty())//清空队列
        Q[0].pop();
    while(!Q[1].empty())
        Q[1].pop();
    step=0;
    Q[0].push(mm);
    Q[1].push(gg);
    while(!Q[0].empty()&&!Q[1].empty())
    {
        step++;
        int flag1=fun(0,3,'M','G'); //走三步
        int flag2=fun(1,1,'G','M');//走一步
        if(flag1==1||flag2==1) //如果有一个人到达对方走过的点则可以走到
        {
            flag=1;
            printf("%d\n",step);
            return ;
        }
    }
    return;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        int u=0;
        flag=0;
        memset(v,0,sizeof(v));
        memset(a,0,sizeof(a));
        for(int i=0; i<n; i++)
        {
            scanf("%s",a[i]);
            for(int j=0; j<m; j++)
            {
                if(a[i][j]=='M')
                {
                    mm.x=i;
                    mm.y=j;   //little erriyue的位置
                }
                if(a[i][j]=='G')
                {
                    gg.x=i;
                    gg.y=j;  //girl friend的位置
                }
                if(a[i][j]=='Z')
                {
                    k[u].xx=i;
                    k[u++].yy=j; //鬼的位置
                }
            }
        }
        bfs();
        if(flag==0)
            printf("-1\n");
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值