-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathRotten_Oranges.cpp
More file actions
48 lines (35 loc) · 1.16 KB
/
Rotten_Oranges.cpp
File metadata and controls
48 lines (35 loc) · 1.16 KB
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
int orangesRotting(vector<vector<int>>& grid) {
int m=grid.size(), n=grid[0].size(), sum=0, ans=0, days=0;
queue<pair<int,int>> q;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(grid[i][j]==2)
q.push({i,j});
if(grid[i][j]!=0)
sum++;
}
}
int dx[4]={0,1,-1,0};
int dy[4]={1,0,0,-1};
while(q.size()!=0)
{
int sz=q.size();
ans+=sz;
while(sz--)
{
int x=q.front().first, y=q.front().second;
q.pop();
for(int i=0;i<4;i++)
{
if(x+dx[i]<0 || y+dy[i]<0 || x+dx[i]>=m || y+dy[i]>=n || grid[x+dx[i]][y+dy[i]]!=1) continue;
grid[x+dx[i]][y+dy[i]]=2;
q.push({x+dx[i], y+dy[i]});
}
}
if(q.size()!=0)
days++;
}
return (sum==ans)?days:-1;
}