Nightmare Ⅱ HDU - 3085

题目链接:Nightmare Ⅱ HDU - 3085

===================================================

Nightmare Ⅱ

Time Limit: 1000MS
Memory Limit: 32768 kB

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

===================================================

算法:BFS+曼哈顿距离

思路:

  • 一开始 突发奇想,一秒走多步?这可以尝试BFS+DFS吗?然后后面wrong了,改成正常思维一个点进一个新队列一下BFS3次,然后还wrong;然后想想因为标记函数(用于避免重复入队),一个点一下走3步,第3步标记的点可能是其他情况的第2步,导致其无法入队并且走出其下一步,导致错误答案;所以需要每个同阶段的点进行一步步判断。
  • 第二点优化,我是看其他人代码学到的,就是evil不需要入队进行随着时间扩散,只要记住两个起点,然后用曼哈顿距离(两个点之间的step数,因为鬼魂无视障碍,所以可以直接使用)。注意点:就是鬼魂无视障碍并且最先走。每一个点判断前需要判断,他位置这个时刻是否有鬼在。
  • 第三点:我的出错点,标记函数并不是用于判断答案的前提条件,而是作为一个避免重复入队操作函数。

===================================================

#include <iostream>
#include <cstring>
#include <queue>
#include <cmath>
#include <vector>

using namespace std;

bool ditu[888][888],boy[888][888],girl[888][888];
int m,n,step;
int Zx[2],Zy[2],num;
int xx[] = {1,0,0,-1};
int yy[] = {0,1,-1,0};
struct node{
    int x,y;
    node(int a,int b):x(a),y(b) {};
};

queue<node> q1,q2;

void init(){
    memset(ditu,true,sizeof ditu);
    memset(boy,true,sizeof boy);
    memset(girl,true,sizeof girl);
    while(!q1.empty()) q1.pop();
    while(!q2.empty()) q2.pop();
    num = 0;step=0;
    cin>>n>>m;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++){
            char ch;ch = getchar();while(ch=='\n'||ch==' ') ch = getchar();
            if(ch=='X') ditu[i][j] = false;
            else if(ch=='Z') Zx[num] = i,Zy[num++] = j;
            else if(ch=='M') q1.push(node(i,j)),boy[i][j] = false;
            else if(ch=='G') q2.push(node(i,j)),girl[i][j] = false;
    }
}

bool check(int x,int y){
    return x>=0&&x<n&&y>=0&&y<m&&ditu[x][y]&&(abs(x-Zx[0])+abs(y-Zy[0])>step*2)&&(abs(x-Zx[1])+abs(y-Zy[1])>step*2);
}

bool bfs(){
    while(!q1.empty()&&!q2.empty()){
        step++;
        int s = q2.size();
        while(s--){
            node p = q2.front();q2.pop();
            if(!check(p.x,p.y)) continue;
            for(int i=0;i<4;i++){
                int x = p.x+xx[i],y = p.y+yy[i];
                if(!check(x,y)) continue;
                if(!boy[x][y]) {cout<<step<<endl;return true;}
                if(girl[x][y]){
                    girl[x][y] = false;q2.push(node(x,y));
                }
            }
        }
        for(int i=0;i<3;i++){
            int s = q1.size();
            while(s--){
                node p = q1.front();q1.pop();
                if(!check(p.x,p.y)) continue;
                for(int i=0;i<4;i++){
                    int x = p.x+xx[i],y = p.y+yy[i];
                    if(!check(x,y)) continue;
                    if(!girl[x][y]) {cout<<step<<endl;return true;}
                    if(boy[x][y]){
                        boy[x][y] = false;q1.push(node(x,y));
                    }
                }
            }
        }
    }
    return false;
}

int main()
{
    int _;cin>>_;
    while(_--){
        init();
        if(!bfs()) puts("-1");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

盐太郎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值