diff --git a/2290_Minimum_Obstacle_Removal_to_Reach_Corner.cpp b/2290_Minimum_Obstacle_Removal_to_Reach_Corner.cpp new file mode 100644 index 0000000..d52246f --- /dev/null +++ b/2290_Minimum_Obstacle_Removal_to_Reach_Corner.cpp @@ -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>& grid) { + //we can use DIJKSTRA's + int m = grid.size(), n = grid[0].size(); + deque> 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}); + } + } + } + } + } +};