当前位置:网站首页>2290. Minimum Obstacle Removal to Reach Corner
2290. Minimum Obstacle Removal to Reach Corner
2022-06-10 15:24:00 【soO_007】
题目:
You are given a 0-indexed 2D integer array grid of size m x n. Each cell has one of two values:
0represents an empty cell,1represents an obstacle that may be removed.
You can move up, down, left, or right from and to an empty cell.
Return the minimum number of obstacles to remove so you can move from the upper left corner (0, 0) to the lower right corner (m - 1, n - 1).
Example 1:

Input: grid = [[0,1,1],[1,1,0],[1,1,0]] Output: 2 Explanation: We can remove the obstacles at (0, 1) and (0, 2) to create a path from (0, 0) to (2, 2). It can be shown that we need to remove at least 2 obstacles, so we return 2. Note that there may be other ways to remove 2 obstacles to create a path.
Example 2:

Input: grid = [[0,1,0,0,0],[0,1,0,1,0],[0,0,0,1,0]] Output: 0 Explanation: We can move from (0, 0) to (2, 4) without removing any obstacles, so we return 0.
Constraints:
m == grid.lengthn == grid[i].length1 <= m, n <= 1052 <= m * n <= 105grid[i][j]is either0or1.grid[0][0] == grid[m - 1][n - 1] == 0
思路:
这题用0-1BFS,显然有障碍的格子cost为1,无障碍的格子cost为0,比较明显的0-1了。建立dist表来记录从0点到{x, y}点的最短距离dist[x][y],建立visited表来记录{x, y}点是否已经更新为最短距离了。之后就是deque双端队列进行BFS,对于4个方向中,cost为1的下一步,放入deque的后端,cost为0的下一步,放入deque的前端,这样就保证了我们从deque中拿出的永远都先是低消耗的点。最后只需要返回dist[m - 1][n - 1]即可。
代码:
class Solution {
public:
int minimumObstacles(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size();
vector<vector<bool>> visited(m, vector<bool>(n, false));
vector<vector<int>> dist(m, vector<int>(n, INT_MAX));
dist[0][0] = 0;
deque<pair<int, int>> q;
vector<pair<int, int>> dirt = { {1, 0}, {-1, 0}, {0 , 1}, {0, -1} };
q.push_back({ 0, 0 });
while (q.size()) {
auto tmp = q.front();
q.pop_front();
int x = tmp.first, y = tmp.second;
if (visited[x][y]) {
continue;
}
visited[x][y] = true;
for (int i = 0; i < dirt.size(); i++) {
int nx = x + dirt[i].first;
int ny = y + dirt[i].second;
if (nx < 0 || nx >= m || ny < 0 || ny >= n) {
continue;
}
dist[nx][ny] = min(dist[nx][ny], dist[x][y] + grid[nx][ny]);
if (grid[nx][ny] == 1) {
q.push_back({ nx, ny });
} else {
q.push_front({ nx, ny });
}
}
}
return dist[m - 1][n - 1];
}
};
边栏推荐
- We media video Hot Ideas sharing
- 一款完整的多用户微信公众平台开发源码,带文档免费分享
- Jiabo gp2120tu label printer installation and use tutorial (PC)
- Detailed explanation of binary search
- VINS理论与代码详解4——初始化
- 洞察的力量
- Cap version 6.1 Release Notice
- 广和通高算力智能模组为万亿级市场5G C-V2X注智
- [reward publicity] [content co creation] issue 16 may Xu sublimation, create a good time! You can also win a gift package of up to 500 yuan if you sign a contract with Huawei cloud Xiaobian!
- uniapp中常用到的方法(部分) - 时间戳问题及富文本解析图片问题
猜你喜欢

VINS理论与代码详解0——理论基础白话篇

Using GDB to quickly read the kernel code of PostgreSQL

初学pytorch踩坑

Explain the opencv function filter2d() in detail and remind you that the operation it does is not convolution but correlation operation

One-way hash function

如何构建以客户为中心的产品蓝图:来自首席技术官的建议

RSA a little bit of thought

Yuntu says that every successful business system cannot be separated from apig

推荐一个好用的设计师导航网址

Sanzi chess (implemented in C language)
随机推荐
Wechat applet slides to the top
Technology sharing | quick intercom, global intercom
反“内卷”,消息称 360 企业安全云将上线“一键强制下班”功能,电脑自动关闭办公软件
How to realize ERP extranet connection?
Problems with database creation triggers
TensorFlow实战Google深度学习框架第二版学习总结-TensorFlow入门
How the autorunner automated test tool creates a project -alltesting | Zezhong cloud test
洞察的力量
3. Encounter the form of handycontrol again
[rust daily] 2022-04-19 performance evaluation of rust asynchronous framework
初学pytorch踩坑
CANN的接口调用流程概述
VINS理論與代碼詳解4——初始化
Jaeger introduces native support for opentelemetry
Hutool使用总结(VIP典藏版)
如何构建以客户为中心的产品蓝图:来自首席技术官的建议
一文带你了解J.U.C的FutureTask、Fork/Join框架和BlockingQueue
微信小程序 滑动到顶部
在什么场景下,我们不能使用箭头函数?
数字化管理中台+低代码,JNPF开启企业数字化转型的新引擎