Skip to content

Commit

Permalink
Create RottenOranges.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
kshitiz11101 authored May 13, 2024
1 parent 7d79246 commit cc787c8
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions Graphs/RottenOranges.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
class Solution {
public:
int orangesRotting(vector<vector<int>>& grid) {
int n=grid.size();
int m=grid[0].size();
// {{},time}

queue<pair<pair<int,int>,int>>q;
int visited[n][m];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(grid[i][j]==2){
q.push({{i,j},0});
visited[i][j]=2;
}
else{
visited[i][j]=0;
}
}
}
int timing=0;
int delrow[]={-1,0,+1,0};
int delcol[]={0,+1,0,-1};
while(!q.empty()){
int r=q.front().first.first;
int c=q.front().first.second;
int t=q.front().second;
timing=max(timing,t);
q.pop();
for(int i=0;i<4;i++){
int nrow=r+delrow[i];
int ncol=c+delcol[i];

if(nrow>=0 && nrow<n && ncol>=0 && ncol<m && visited[nrow][ncol]==0 && grid[nrow][ncol]==1){
q.push({{nrow,ncol},t+1});
visited[nrow][ncol]=2;
// move++;
}
}


}

for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(visited[i][j]!=2 && grid[i][j]==1){
return -1;
}
}
}
return timing;
}
};

0 comments on commit cc787c8

Please sign in to comment.