-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotten_oranges.cpp
43 lines (43 loc) · 1.31 KB
/
rotten_oranges.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Solution {
public:
int tm=0;
int orangesRotting(vector<vector<int>>& grid) {
int n=grid.size();
int m=grid[0].size();
//{{r,c},t}
int drow[] = {-1, 0, +1, 0};
int dcol[] = {0, 1, 0, -1};
queue<pair<pair<int,int>,int>>q;
vector<vector<int>>vis(n,vector<int>(m,0));
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(grid[i][j]==2){
q.push({{i,j},0});
}
}
}
//BFS starts
while(!q.empty()){
int r=q.front().first.first;
int c=q.front().first.second;
vis[r][c]=2;
int t=q.front().second;
tm=max(tm,t);
q.pop();
for(int i=0;i<4;i++){
int nrow=r+drow[i];
int ncol=c+dcol[i];
if(nrow>=0&&ncol>=0&&nrow<vis.size()&&ncol<vis[0].size()&&grid[nrow][ncol]==1&&!vis[nrow][ncol]){
q.push({{nrow,ncol},t+1});//push those who are now rotten
vis[nrow][ncol]=2;//to simultaneously make it visited
}
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(vis[i][j]!=2&&grid[i][j]==1)return -1;
}
}
return tm;
}
};