2022.1.11 力扣-每日一题-逃离大迷宫

题目描述:

在一个 106 x 106 的网格中,每个网格上方格的坐标为 (x, y) 。

现在从源方格 source = [sx, sy] 开始出发,意图赶往目标方格 target = [tx, ty] 。数组 blocked 是封锁的方格列表,其中每个 blocked[i] = [xi, yi] 表示坐标为 (xi, yi) 的方格是禁止通行的。

每次移动,都可以走到网格中在四个方向上相邻的方格,只要该方格 不 在给出的封锁列表 blocked 上。同时,不允许走出网格。

只有在可以通过一系列的移动从源方格 source 到达目标方格 target 时才返回 true。否则,返回 false。

样例:

方法一:

const int Edge = 1000000;
const int Found = 1;    //找到了目标方格
const int Over = 2;     //没有被包围住
const int Surround = 3;  //被包围住了
class Solution {
public:
    int dir[4][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};
    bool isEscapePossible(vector<vector<int>>& blocked, vector<int>& source, vector<int>& target) {
        int n = blocked.size();
        //障碍物的数量小于两个时不可能包围住一个方格
        if (n <= 1) return true;
        //将障碍方格转换为哈希,方便查找
        set<pair<int, int>> blockedset;
        for (int i = 0; i < n; i++)
        {
            int a = blocked[i][0];
            int b = blocked[i][1];
            blockedset.emplace(a, b);
        }
        //Max为由障碍物包围的圈中最多的非障碍物的方格数量
        int Max = (n - 1) * n / 2;
        //检测从source出发是否能够到达target,或者是否有被围住
        int result = bfs(blockedset, source, target, Max);
        if (result == Found)    //从source出发能够在Max数量的方格内到达target
        {
            return true;
        }
        else if (result == Surround)    //如果source方格被包围住了,那么不可能到达target
        {
            return false;
        }
        //如果source方格没有被围住,那么检测从target出发是否能够到达source,或者是否有被围住
        //只要不等于Surround,即能够找到source或者target没有被围住,那么就是true
        return bfs(blockedset, target, source, Max) != Surround;
    }

    int bfs(set<pair<int, int>>& blockedset, vector<int>& source, vector<int>& target, int Max)
    {
        //记录访问过的方格
        set<pair<int, int>> visited;
        visited.emplace(source[0], source[1]);
        //bfs所要用到的队列
        queue<pair<int, int>> que;
        que.emplace(source[0], source[1]);
        while (!que.empty())
        {
            int n = que.size();
            for (int i = 0; i < n; i++)
            {
                int curx = que.front().first;
                int cury = que.front().second;
                que.pop();
                for (int j = 0; j < 4; j++)
                {
                    int newx = curx + dir[j][0];
                    int newy = cury + dir[j][1];
                    pair<int, int> p(newx, newy);
                    //如果下一方格在网格内,并且不是障碍物,也不是到达过的方格
                    if (newx >= 0 && newx < Edge && newy >= 0 && newy < Edge && !blockedset.count(p) && !visited.count(p))
                    {
                        //找到了target
                        if (newx == target[0] && newy == target[1])
                        {
                            return Found;
                        }
                        visited.insert(p);
                        //若所到达的方格数量已经超过了Max,说明不会被包围住
                        if (visited.size() > Max) 
                        {
                            return Over;
                        }
                        que.push(p);
                    }
                }
            }
        }
        return Surround;
    }
};

这道题思路还是挺简单的,我一开始看到就想到了使用bfs,后来一看数据量发现不对呀,10^6的方格数量,不管是单项还是双向的bfs也必超时啊,然后就想到了障碍物的数量才200而已,对于source和target两个方格而言,只要其中一个被包围住了,那么source就不可能到达target。

但是思路归思路,怎么判断source和target是否有被包围住?我思索了一会儿就默默看题解去了:力扣

这个判断是否被包围的方法挺巧妙的,还要用到点数学能力,虽然看着简单,但我自己来想的话不知猴年马月才能想出来= =,知道如何判断包围之后就简单了,其余就是正常的bfs套路。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值