diff --git a/Graphs/MinimumCosttoMakeatLeastOneValidPathinaGrid.cpp b/Graphs/MinimumCosttoMakeatLeastOneValidPathinaGrid.cpp new file mode 100644 index 0000000..f13e488 --- /dev/null +++ b/Graphs/MinimumCosttoMakeatLeastOneValidPathinaGrid.cpp @@ -0,0 +1,35 @@ +class Solution { +public: + int minCost(vector>& grid) { + int n=grid.size(),m=grid[0].size(); + + priority_queue>, vector>>, greater<>> pq; + pq.push({0,{0,0}}); + vector> visited(n, vector(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=0 && ncol