当前位置:网站首页>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];
}
};
边栏推荐
- Using GDB to quickly read the kernel code of PostgreSQL
- Technology sharing | quick intercom, global intercom
- 如何写一个全局的 Notice 组件?
- HKU and NVIDIA | factuality enhanced language models for open ended text generation
- RSA a little bit of thought
- Comply with medical reform and actively layout -- insight into the development of high-value medical consumables under the background of centralized purchase 2022
- 百度开源ICE-BA安装运行总结
- How to open an account for agricultural futures? Are there any financial conditions?
- 顺应医改,积极布局——集采背景下的高值医用耗材发展洞察2022
- 从“初代播种”到“落地生花”,广和通在5G商用三年间做了什么?
猜你喜欢

数据库创建触发器的问题

视觉SLAM常见的QR分解SVD分解等矩阵分解方式求解满秩和亏秩最小二乘问题(最全的方法分析总结)

Fast detection of short text repetition rate

数字化管理中台+低代码,JNPF开启企业数字化转型的新引擎

4. Meet panuon again UI. Title bar of silver form

ORB_ Slam2 visual inertial tight coupling positioning technology route and code explanation 1 - IMU flow pattern pre integration

Remote monitoring and data acquisition solution

Technology sharing | quick intercom, global intercom

Using GDB to quickly read the kernel code of PostgreSQL

Méthodes couramment utilisées dans uniapp - TIMESTAMP et Rich Text Analysis picture
随机推荐
Summary of 5 years' experience in ERP odoo privilege management system setup
Technology sharing | quick intercom, global intercom
Golang []byte to file
The power of insight
产品设计软件Figma用不了,国内有哪些相似功能的软件
Using GDB to quickly read the kernel code of PostgreSQL
ORB_ Slam2 visual inertial tight coupling positioning technology route and code explanation 2 - IMU initialization
uniapp中常用到的方法(部分) - 时间戳问题及富文本解析图片问题
json. Load (s) and json dump(s)
rk3399_ 9.0 first level menu Network & Internet without setting
RSA a little bit of thought
Overview of cann interface calling process
[cloud native | kubernetes] in depth RC, RS, daemonset, statefulset (VII)
顺应医改,积极布局——集采背景下的高值医用耗材发展洞察2022
Software intelligence: formal rules of AAAS system metrics and grammars
点投影到平面上的方法总结
ADA logics:cri-o overall safety audit project
How to open an account for agricultural futures? Are there any financial conditions?
After class assignment for module 8 of phase 6 of the construction practice camp
自媒体视频热门思路分享