-
Notifications
You must be signed in to change notification settings - Fork 36
/
Rotton-orange.cpp
56 lines (49 loc) · 1.46 KB
/
Rotton-orange.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
44
45
46
47
48
49
50
51
52
53
54
55
56
class Solution {
public:
int orangesRotting(vector<vector<int>>& grid) {
//make a ques of pair ans integer
// pair conting i , j -->row number and colmn no.
// and integer is 0 initially
queue<pair<pair<int,int>,int>>q;
vector<vector<int>>img =grid;
int n = grid.size();
int m = grid[0].size();
for(int i =0; i<n ; i++){
for(int j =0 ; j<m ; j++){
if(img[i][j]==2){
q.push({{i,j},0});
img[i][j]=2;
}
}
}
int ans =0;
// apply bfs
while(!q.empty()){
int r = q.front().first.first;
int c = q.front().first.second;
int t = q.front().second;
ans = max( ans , t);
q.pop();
int row[] ={-1,0,1,0};
int col[] ={0,1,0,-1};
for(int i =0 ; i<4; i++){
int x , y;
x = r+ row[i];
y = c+ col[i];
if( x>=0 && x<n && y>=0 && y<m && img[x][y]==1){
q.push({{x,y},t+1});
img[x][y]=2;
}
}
}
// check there is no fresh orange
for(int i =0 ; i<n ;i++){
for(int j =0; j<m ; j++){
if(img[i][j]==1){
return -1;
}
}
}
return ans;
}
};