-
Notifications
You must be signed in to change notification settings - Fork 36
/
RottenOranges.cpp
47 lines (45 loc) · 1.24 KB
/
RottenOranges.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
class Solution
{
public:
int orangesRotting(vector<vector<int>>& grid)
{
int n = grid.size();
int m = grid[0].size();
queue<pair<int, int>> q;
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
if(grid[i][j]==2)
q.push({i, j});
int x[] = {0, 0, -1, 1};
int y[] = {-1, 1, 0, 0};
int time = 0;
while(!q.empty())
{
int size = q.size();
bool anyRotten = false;
while(size-->0)
{
pair<int, int> p = q.front();
q.pop();
for(int k=0; k<4; k++)
{
int i = p.first + x[k];
int j = p.second + y[k];
if(i>=0 && i<n && j>=0 && j<m && grid[i][j]==1)
{
anyRotten = true;
grid[i][j] = 2;
q.push({i, j});
}
}
}
if(anyRotten)
time++;
}
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
if(grid[i][j]==1)
return -1;
return time;
}
};