-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathRotting_Oranges.cpp
106 lines (93 loc) · 2.43 KB
/
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include<bits/stdc++.h>
using namespace std;
class Solution
{
public:
//Function to find minimum time required to rot all oranges.
int orangesRotting(vector<vector<int>>& grid)
{
queue<pair<int,int>>q;
for(int i=0;i<grid.size();i++)
{
for(int j=0;j<grid[0].size();j++)
{
if(grid[i][j]==2)
{
q.push({i,j});
}
}
}
int c=0;
// int c=-1,d=-1;
q.push({-1,-1});
int p=grid.size();
int qr=grid[0].size();
while(!q.empty())
{
int n=q.size();
bool flag=false;
while(q.front().first!=-1 && q.front().second!=-1)
{
int x=q.front().first;
int y=q.front().second;
if(x+1<p && grid[x+1][y]==1)
{
grid[x+1][y]=2;
q.push({x+1,y});
flag=true;
}
if(y+1<qr && grid[x][y+1]==1)
{
grid[x][y+1]=2;
q.push({x,y+1});
flag=true;
}
if(y-1>=0 && grid[x][y-1]==1)
{
grid[x][y-1]=2;
q.push({x,y-1});
flag=true;
}
if(x-1>=0 && grid[x-1][y]==1)
{
grid[x-1][y]=2;
q.push({x-1,y});
flag=true;
}
q.pop();
}
q.pop();
if(!q.empty())
{
q.push({-1,-1});
}
if(flag)
c++;
}
for(int i=0;i<grid.size();i++)
{
for(int j=0;j<grid[0].size();j++)
{
if(grid[i][j]==1)
{
return -1;
}
}
}
return c;
}
};
int main(){
int n, m;
cin >> n >> m;
vector<vector<int>>grid(n, vector<int>(m, -1));
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
cin >> grid[i][j];
}
}
Solution obj;
int ans = obj.orangesRotting(grid);
cout << ans << "\n";
return 0;
}