当前位置:网站首页>2290. Minimum Obstacle Removal to Reach Corner
2290. Minimum Obstacle Removal to Reach Corner
2022-06-10 15:38:00 【soO_ 007】
subject :
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
Ideas :
This question uses 0-1BFS, A grid with obvious obstacles cost by 1, Barrier free grid cost by 0, Quite obvious 0-1 了 . establish dist Table to record from 0 Point to {x, y} The shortest distance of a point dist[x][y], establish visited Table to record {x, y} Whether the point has been updated to the shortest distance . After is deque Double ended queue BFS, about 4 In all directions ,cost by 1 Next step , Put in deque Backend ,cost by 0 Next step , Put in deque The front end of the , This ensures that we start from deque Always take out the low consumption points first . Finally, just go back dist[m - 1][n - 1] that will do .
Code :
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];
}
};
边栏推荐
- 苹果式中文:似乎表达清楚意思了,懂了没完全懂的苹果式宣传文案
- Applet network request promise
- ORB_SLAM2视觉惯性紧耦合定位技术路线与代码详解0——整体框架与理论基础知识
- 音视频处理三剑客之 AEC:回声产生原因及回声消除原理
- Digital management medium + low code, jnpf opens a new engine for enterprise digital transformation
- 【MySQL基础】
- Cube 技术解读 | Cube 渲染设计的前世今生
- How the terminator sets the font to display different colors
- Create a space of local value together. In 2022, China successfully held the "one hundred cities tour · Ningbo Station" for commercial distribution
- Hessian matrix of convex function and Gauss Newton descent method
猜你喜欢

One-way hash function

智能电网终极Buff | 广和通模组贯穿“发、输、变、配、用”全环节

How to realize ERP extranet connection?

Vins Theory and Code detail 4 - Initialization

How the autorunner automated test tool creates a project -alltesting | Zezhong cloud test

这几个垂直类小众导航网站,你绝对不会想错过

Guanghetong cooperates with China Mobile, HP, MediaTek and Intel to build 5g fully connected PC pan terminal products

Méthodes couramment utilisées dans uniapp - TIMESTAMP et Rich Text Analysis picture

Tensorflow actual combat Google deep learning framework second edition learning summary tensorflow introduction

opencv神经网络库之SVM和ANN_MLP的使用
随机推荐
音视频处理三剑客之 AEC:回声产生原因及回声消除原理
[rust daily] first release of mnemos on April 20, 2022
Explore the secrets behind the open source data visualization development platform flyfish!
How to write a global notice component?
推荐一个好用的设计师导航网址
Huawei cloud SRE deterministic O & M introduction
Golang []byte to file
Create a space of local value together. In 2022, China successfully held the "one hundred cities tour · Ningbo Station" for commercial distribution
terminator如何设置字体显示不同颜色
Kubernetes 1.24:statefulset introduces maxunavailable copies
如何构建以客户为中心的产品蓝图:来自首席技术官的建议
Vins theory and code explanation 0 -- theoretical basis in vernacular
Summary of methods for point projection onto a plane
After class assignment for module 8 of phase 6 of the construction practice camp
Vins theory and code explanation 4 - initialization
Applet network request promise
Wechat applet color gradient
Click to unlock "keyword" of guanghetong 5g module
影刀RPA学习和遇见excel部分问题解决方式
凸函数的Hessian矩阵与高斯牛顿下降法增量矩阵半正定性的理解