HDOJ 3085 Nightmare Ⅱ----双向BFS

题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3085

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

题目大意: 输入一个n*m的矩阵,X表示墙,z表示幽灵,G和M表示2个人,M每秒最多可以移动3个格子,G1个格子,每次移动只能上下左右移动,G和M不可以穿墙,幽灵每秒会先于G和M在距离小于等于2的格子上产生分身,幽灵和人如果位于同一格子,幽灵会kill掉人,判断G和M能否相遇,返回相遇时消耗的最小时间,不能相遇返回-1.

题解: 这个题感觉挺有难度的,我没有什么好的思路,基本上都是参考别人的代码。 (参考链接https://blog.csdn.net/YHYYXT/article/details/50603722

一般先想到的是预处理Z,求出Z占领方格ij时的时间,但是,在这一题没必要,MG可以边走边判断,假设M/G走到方格str[i][j]时耗时为t,距离两个原始鬼魂的最短距离是dis,如果dis<t*2,那M/G肯定无法到达ij这个方格,否则就是可以到达

还有一点就是什么时候MG走or不走,想要输出的时间最少,那必定是两个人尽可能的多的走路,唯一的一个区别就是最后一步,如果他们最后的距离就只有3,那只要M走就可以了,如果他们最后的距离是1,那只要G走就可以了

 

#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;

char graph[805][805];   // 地图,从下标1开始记录
int time;       // 时间
int n,m;        // 地图行列数
int t;
int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};    // 方向
struct node {
    // 点
    int x,y;
}gg,mm,zz[2];           // g,m,和ghost
queue<node> q[2],qt;    // gg和mm队列,临时队列
/**
 *  判断某个点是否可能满足条件
 **/
bool isOk(node t) {
    if(t.x < 1 || t.x > n || t.y < 1 || t.y > m)
        return false;   // 越界
    if(graph[t.x][t.y] == 'X' || graph[t.x][t.y] == '\0')
        return false;   // 墙
    for (int i = 0; i < 2; i++) {
        if(2*time >= abs(t.x-zz[i].x) + abs(t.y-zz[i].y))
            return false;     // 距离幽灵太近
    }    
    
    return true;
}

/**
 * 广度优先搜索 
 * mark:标记是g还是m
 * num: 一秒最多走的步数
 */
bool bfs(int mark,int num,char start,char endd) {
    node a,b;   // 临时节点
    qt = q[mark];    // 临时队列
    for (int i = 0; i < num; i++) {
        // 遍历所有可能到达的点
        while(!qt.empty()) {
            a = qt.front(),qt.pop(),q[mark].pop();
            if(!isOk(a)) continue;
            for (int i = 0; i < 4; i++) {
                // 4个方向
                b = a;
                b.x += dir[i][0],b.y += dir[i][1];
                if(!isOk(b) || graph[b.x][b.y] == start)
                    continue;         // start避免重复走相同的路
                if(graph[b.x][b.y] == endd)    
                    return true;     // 其中一人能到达另一人可以到达的点
                graph[b.x][b.y] = start;    // 标记走过的点
                q[mark].push(b);
            }
        }
        qt = q[mark];
    }
    return false;
}

/**
 * 计算结果 
 */
int solve() {
    // 初始化
    time = 0;
    while(!q[0].empty()) q[0].pop();
    while(!q[1].empty()) q[1].pop();
    while(!qt.empty()) qt.pop();
    
    q[0].push(mm);
    q[1].push(gg);
    
    while(!q[0].empty() && !q[1].empty()) {
        time++;
        bool flag1 = bfs(0,3,'M','G');
        bool flag2 = bfs(1,1,'G','M');
        if(flag1 || flag2)
            return time;
    }
    return -1;
}

int main() {
    scanf("%d",&t);
    while(t--) {
        scanf("%d%d",&n,&m);
        memset(graph,'X',sizeof(graph));
        for(int k = 0,i = 1;i <= n;i++) {
            scanf("%s",graph[i]+1);
            for(int j = 1;j <= m;j++) {
                if(graph[i][j] == 'M') mm.x = i,mm.y = j;
                if(graph[i][j] == 'G') gg.x = i,gg.y = j;
                if(graph[i][j] == 'Z') zz[k].x = i,zz[k].y = j,k++;
                
            }
        }
        printf("%d\n",solve());
        
    }
    
    
    return 0;
}










 

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值