-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path994. Rotting Oranges.cpp
60 lines (50 loc) · 1.69 KB
/
994. Rotting 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class Solution {
public:
int orangesRotting(vector<vector<int>>& arr) {
int i,j,ans=-1, r=arr.size(), c = arr[0].size(), totalOranges = 0;
queue<pair<int,int>> q;
for(i=0; i<r; i++){
for(j=0; j<c; j++){
if(arr[i][j] == 1)
totalOranges++;
else if(arr[i][j] == 2)
q.push({i,j});
}
}
if(totalOranges == 0)
return 0;
while(!q.empty()){
ans++;
int s = q.size();
if(totalOranges == 0)
return ans;
while(s--){
auto p = q.front();
int x = p.first, y = p.second;
arr[x][y] = -1;
if(x>0 && arr[x-1][y] == 1){
arr[x-1][y] = 2;
q.push({x-1,y});
totalOranges--;
}
if(x<r-1 && arr[x+1][y] == 1){
arr[x+1][y] = 2;
q.push({x+1,y});
totalOranges--;
}
if(y>0 && arr[x][y-1] == 1){
arr[x][y-1] = 2;
q.push({x,y-1});
totalOranges--;
}
if(y<c-1 && arr[x][y+1] == 1){
arr[x][y+1] = 2;
q.push({x,y+1});
totalOranges--;
}
q.pop();
}
}
return -1;
}
};