LeetCode 2290. Minimum Obstacle Removal to Reach Corner

🔗 https://leetcode.com/problems/minimum-obstacle-removal-to-reach-corner

题目

  • 给 m * n 的二维数组,0 代表空 cell,1 代表有障碍物
  • 从 0,0 到 m-1,n-1,需要至少移掉几个障碍物
  • 格子间的路径只能是上下左右

思路

  • 第一反应是 dfs + 剪枝,后来想着还没正儿八经写过 dijkstra,试一下 dijkstra
  • 对 cell 进行编码,从二维降为一维度,[row_index, col_index]→ row_index * col_num + col_index
  • 定义一个最小堆,记录当前 cell 的编号,以及起始 cell 到当前 cell 的最短距离 len,用堆顶扩展可触达的 cell
    • 如果到了目的地,返回 len,
    • 如果没到达过,更新 len,如果该 cell 是障碍物, len+1,入堆
    • 如果到达过,且历史到达的 distance 比当前到达的大,重新入堆

代码

cpp 复制代码
class Solution {
public:
    int minimumObstacles(vector<vector<int>>& grid) {
               auto minHeapCompare = [](pair<int, int> left, pair<int, int> right) {
            return left.second > right.second; // 自定义比较器,建立最小堆
        };
        std::priority_queue<pair<int, int>, std::vector<pair<int, int>>,
                            decltype(minHeapCompare)>
            heap(minHeapCompare);

        int m = grid.size(), n = grid[0].size();
        int goal = m * n - 1;
        vector<int> visit(goal + 1, -1);
        visit[0] = 0;
        // init node
        int dst = 0, len = 0;
        pair<int, int> path;
        path = make_pair(dst, len);
        heap.push(path);
        vector<vector<int>> dir = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
        // dijkstra
        while (heap.empty() == false) {
            path = heap.top();
            heap.pop();
            dst = path.first, len = path.second;
            if (dst == goal) return len;
            if (visit[dst] != -1 && visit[dst] < len) continue;
            for (int i = 0; i < dir.size(); i++) {
                int row = dst / n, col = dst % n;
                row += dir[i][0]; col += dir[i][1];
                if (row >= m || row < 0 || col >= n || col < 0) continue;
                int new_dst = row * n + col;
                int new_len = len;
                if (grid[row][col]) new_len++;
                if (visit[new_dst] == -1 || visit[new_dst] > new_len) {
                    visit[new_dst] = new_len;
                    path = make_pair(new_dst, new_len);
                    heap.push(path);
                }                
            }
        }
        return 0;
    }
};
相关推荐
嘻嘻哈哈的zl38 分钟前
初级数据结构:栈和队列
c语言·开发语言·数据结构
小王努力学编程39 分钟前
【C++篇】哈希表
数据结构·哈希算法·散列表
君义_noip41 分钟前
信息学奥赛一本通 1607:【 例 2】任务安排 2 | 洛谷 P10979 任务安排 2
算法·动态规划·信息学奥赛·斜率优化
因兹菜1 小时前
[LeetCode]day4 977.有序数组的平方
数据结构·算法·leetcode
weixin_537590451 小时前
《C程序设计》第六章练习答案
c语言·c++·算法
_周游2 小时前
【数据结构】_时间复杂度相关OJ(力扣版)
数据结构
码农小苏242 小时前
K个不同子数组的数目--滑动窗口--字节--亚马逊
java·数据结构·算法
独自破碎E2 小时前
【4】阿里面试题整理
java·开发语言·算法·排序算法·动态规划
涛ing7 小时前
32. C 语言 安全函数( _s 尾缀)
linux·c语言·c++·vscode·算法·安全·vim
xrgs_shz8 小时前
MATLAB的数据类型和各类数据类型转化示例
开发语言·数据结构·matlab