Skip to content

Commit

Permalink
Create MinimumObstacleRemovaltoReachCorner.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
kshitiz11101 authored Jan 18, 2025
1 parent 5155978 commit b932e1b
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Graphs/MinimumObstacleRemovaltoReachCorner.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class Solution {
public:
int minimumObstacles(vector<vector<int>>& grid) {
int n=grid.size(),m=grid[0].size();
priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<>> pq;
pq.push({0,{0,0}});
vector<vector<int>> visited(n, vector<int>(m, 1e9));
visited[0][0]=0;
int delrow[]={1,-1,0,0};
int delcol[]={0,0,-1,1};
while(!pq.empty()){
int cost=pq.top().first;
int row=pq.top().second.first;
int col=pq.top().second.second;
pq.pop();
if(row==n-1 && col==m-1){
return cost;
}

for(int ind=0;ind<4;ind++){
int nrow=row+delrow[ind];
int ncol=col+delcol[ind];

if(nrow>=0 && nrow<n && ncol>=0 && ncol<m){

int ncost=cost+grid[nrow][ncol];
if(ncost<visited[nrow][ncol]){
visited[nrow][ncol]=ncost;
pq.push({ncost,{nrow,ncol}});
}


}
}
}

return 0;
}
};

0 comments on commit b932e1b

Please sign in to comment.