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

解题报告:

1:男生可以走0 1 2 3步。女生可以走0 1步。鬼可以走 0 1 2步。

2:Map数组保存地图。visit[0][N][N]标记男的,visit[1][N][N]标记女的。dir方向数组。g表示鬼的数组。point结构体记录坐标用做bfs用。Q[0]男生队列,Q[1]女生队列。

3:check函数曼哈顿距离来判断一个点在某时刻鬼是否到达。代码中的op^1代表看对方是否走过这个点,如果两人都走过就可以结束了,已找到最短时间。因为男女都可以呆在原地不动。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 800+10;
char Map[N][N];
bool visit[2][N][N];
ll dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0}, g[2][2];
struct point{
    ll x, y;
}p1, p2;
queue<point> Q[2];
bool check(ll x, ll y, ll time){
    if(fabs(x - g[0][0]) + fabs(y - g[0][1]) <= 2*time)return 0;
    if(fabs(x - g[1][0]) + fabs(y - g[1][1]) <= 2*time)return 0;
    return 1;
}
bool bfs(ll op, ll n, ll m, ll time){
    ll len = Q[op].size();
    while(len--){
        p1 = Q[op].front();
        Q[op].pop();
        if(!check(p1.x, p1.y, time))continue;
        for(ll i=0; i<4; ++i){
            ll xx = p1.x+dir[i][0];
            ll yy = p1.y+dir[i][1];
            if(xx < 0 || yy < 0 || xx >= n || yy >= m || Map[xx][yy] == 'X' || visit[op][xx][yy])continue;
            if(!check(xx, yy, time))continue;
            if(visit[op^1][xx][yy])return 1;
            p2.x = xx, p2.y = yy, visit[op][xx][yy] = 1;
            Q[op].push(p2);
        }
    }
    return 0;
}
ll solve(ll n, ll m, ll mX, ll mY, ll gX, ll gY){
    for(ll i=0; i<2; ++i)while(!Q[i].empty())Q[i].pop();
    memset(visit, 0, sizeof(visit));
    p1.x = mX, p1.y = mY, Q[0].push(p1);
    p1.x = gX, p1.y = gY, Q[1].push(p1);
    visit[0][mX][mY] = 1, visit[1][gX][gY] = 1;
    ll time = 1;
    while(!Q[0].empty() || !Q[1].empty()){
        for(ll i=0; i<3; ++i)
            if(bfs(0, n, m, time))return time;
        if(bfs(1, n, m, time))return time;
        ++time;
    }
    return -1;
}
int main(){
    ll t, n, m, mX, mY, gX, gY;
    scanf("%lld", &t);
    while(t--){
        scanf("%lld%lld", &n, &m);
        ll cnt = 0;
        for(ll i=0; i<n; ++i)scanf("%s", Map+i);
        for(ll i=0; i<n; ++i)
            for(ll j=0; j<m; ++j){
                if(Map[i][j] == 'M')mX = i, mY = j;
                if(Map[i][j] == 'G')gX = i, gY = j;
                if(Map[i][j] == 'Z')g[cnt][0] = i, g[cnt++][1] = j;
            }
        printf("%lld\n", solve(n, m, mX, mY, gX, gY));
    }
    return 0;
}
发布了190 篇原创文章 · 获赞 9 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/jun_____/article/details/104260274