美文网首页
1036. 逃离大迷宫

1036. 逃离大迷宫

作者: 来到了没有知识的荒原 | 来源:发表于2022-01-13 17:30 被阅读0次

1036. 逃离大迷宫

得能意识到最优围堵的方法是下图。最优的围堵方法也就是使得我们需要搜索最多位置的方法。

image.png
const int N = 1e6;
class Solution {
public:
    int bfs(set<pair<int,int>>& bs, vector<int>& source, vector<int>& target) {
        int n = bs.size();
        set<pair<int,int>>S;
        queue<pair<int,int>>Q;
        Q.push({source[0], source[1]});
        S.insert({source[0], source[1]});
        int dx[]={-1,0,1,0}, dy[]={0,1,0,-1};
        while(Q.size()) {
            auto u=Q.front();
            Q.pop();
            if(u.first==target[0] && u.second==target[1])return 0;
            for(int i=0;i<4;i++){
                int a=u.first+dx[i], b=u.second+dy[i];
                if(a>=0 && a<N && b>=0 && b<N && bs.count({a,b})==0 && S.count({a,b})==0){
                    S.insert({a,b});
                    if(a==target[0] && b==target[1])return 0;
                    if(S.size()>n*(n-1)/2)return 2;
                    Q.push({a,b});
                }
            }
        }
        if(S.size()<=n*(n-1)/2) return 1;
        return -1;
        // 0: 搜到了,1: 被block,2: 无block
    }
    bool isEscapePossible(vector<vector<int>>& blocked, vector<int>& source, vector<int>& target) {
        set<pair<int,int>>bs;
        for(auto p:blocked)bs.insert({p[0], p[1]});
        int res1=bfs(bs,source,target);
        if(res1==0)return true;
        else if(res1==1)return false;
        int res2=bfs(bs,target,source);
        if(res2==2)return true;
        return false;
    }
};

相关文章

  • 1036. 逃离大迷宫

    1036. 逃离大迷宫[https://leetcode-cn.com/problems/escape-a-lar...

  • 2022-01-11 1036. 逃离大迷宫

    java版本:

  • LeetCode #1036 Escape a Large Ma

    1036 Escape a Large Maze 逃离大迷宫 Description:There is a 1 m...

  • 【BFS】HDU_1728_逃离迷宫

    逃离迷宫Time Limit: 1000/1000 MS (Java/Others) Memory Limi...

  • 重叠迷宫

    不要抱我太紧 被子里没有新鲜空气 我曾在梦里死去 逃离这伊甸园 逃离你 逃离你 我以为我不会哭 独自走在迷宫 偶尔...

  • 随笔

    为逃离险境,四人同心合力在迷宫般的船舱中寻找

  • 诗-2

    睡前想你的种种 入梦 用最纯粹的眼神 领悟 逃离陷入的重重 迷宫 不愿醒来的情绪 谁懂 ​​

  • 我好想逃离

    你有没有一个时刻特别想逃离, 逃离现在繁琐的生活, 逃离每天繁杂的家务, 逃离七大姑八大姨的无效社交, 逃离每天带...

  • 亲子阅读成长记录第217天

    2017.11.19 星期天 晴 书单:《儿童世界历史迷宫大冒险:勇闯金字塔》、《儿童世界历史迷宫大...

  • 时速大逃离

    许久未曾进省城了,对这大都市或许还是少了了解!不曾想在即将到来的普天同庆的祖国奶奶生日来临之际,在省城上演一出《时...

网友评论

      本文标题:1036. 逃离大迷宫

      本文链接:https://www.haomeiwen.com/subject/zhancrtx.html