Skip to content

Commit

Permalink
Create 2290_Minimum_Obstacle_Removal_to_Reach_Corner.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Nothing-avil authored Nov 28, 2024
1 parent 8036e3d commit 021eeb9
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions 2290_Minimum_Obstacle_Removal_to_Reach_Corner.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// ███████╗ █████╗ ███╗ ██╗ ██████╗ █████╗ ██████╗ ██████╗ ██╗ ██╗
// ██╔════╝ ██╔══██╗ ████╗ ██║ ██╔══██╗ ██╔══██╗ ██╔══██╗ ██╔══██╗ ██║ ██║
// ███████╗ ███████║ ██╔██╗ ██║ ██║ ██║ ███████║ ██████╔╝ ██████╔╝ ███████║
// ╚════██║ ██╔══██║ ██║╚██╗██║ ██║ ██║ ██╔══██║ ██╔═██╗ ██╔══██╗ ██╔══██║
// ███████║ ██║ ██║ ██║ ╚████║ ██████╔╝ ██║ ██║ ██║ ██╗ ██████╔╝ ██║ ██║
// ╚══════╝ ╚═╝ ╚═╝ ╚═╝ ╚═══╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝
#pragma GCC optimize("Ofast", "inline", "ffast-math", "unroll-loops","no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native", "f16c")
auto init = []() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
return 'c';
}();
class Solution {
public:
int minimumObstacles(vector<vector<int>>& grid) {
//we can use DIJKSTRA's
int m = grid.size(), n = grid[0].size();
deque<tuple<int, int, int>> q{{0, 0, 0}};
bool vis[m][n];
memset(vis, 0, sizeof vis);
int dirs[5] = {-1, 0, 1, 0, -1};
while (1) {
auto [i, j, k] = q.front();
q.pop_front();
if (i == m - 1 && j == n - 1) {
return k;
}
if (vis[i][j]) {
continue;
}
vis[i][j] = true;
for (int h = 0; h < 4; ++h) {
int x = i + dirs[h], y = j + dirs[h + 1];
if (x >= 0 && x < m && y >= 0 && y < n) {
if (grid[x][y] == 0) {
q.push_front({x, y, k});
} else {
q.push_back({x, y, k + 1});
}
}
}
}
}
};

0 comments on commit 021eeb9

Please sign in to comment.