Skip to content

Commit

Permalink
Create MinimumCosttoMakeatLeastOneValidPathinaGrid.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
kshitiz11101 authored Jan 18, 2025
1 parent 15bd229 commit 5155978
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions Graphs/MinimumCosttoMakeatLeastOneValidPathinaGrid.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Solution {
public:
int minCost(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[]={0,0,1,-1};
int delcol[]={1,-1,0,0};
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];
int ncost=cost+(ind+1!=grid[row][col]);
if(nrow>=0 && nrow<n && ncol>=0 && ncol<m && ncost<visited[nrow][ncol]){
visited[nrow][ncol]=ncost;
pq.push({ncost,{nrow,ncol}});

}
}
}

return 0;
}
};

0 comments on commit 5155978

Please sign in to comment.