-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRotten Oranges.cpp
55 lines (48 loc) · 1.21 KB
/
Rotten 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
class node{
public:
int x,y,time;
node(int _x, int _y, int _time){
x = _x;
y = _y;
time = _time;
}
};
int solve(vector<vector<int>> grid){
int m = grid.size(), n = grid[0].size();
int ans = 0;
int fresh = 0, rotten = 0;
queue<node> q;
//Insert all rotten oranges in queue, count Fresh oranges
for(int i = 0 ; i < m ; i++){
for(int j = 0 ; j < n ; j++){
if(grid[i][j] == 1)
fresh+=1;
else if(grid[i][j] == 2){
rotten+=1;
q.push(node(i,j,0));
}
}
}
if(fresh==0) return 0;
int dx[] = {0,1,-1,0};
int dy[] = {1,0,0,-1};
while(!q.empty()){
node ele = q.front();
q.pop();
int x = ele.x;
int y = ele.y;
int time = ele.time;
ans = max(ans, time);
for(int a = 0 ; a < 4 ; a++){
int nx = x+dx[a];
int ny = y+dy[a];
if(nx>=0 && nx<m && ny >= 0 && ny < n && grid[nx][ny] == 1){
grid[nx][ny] = 2;
fresh-=1;
q.push(node(nx, ny, time+1));
}
}
}
if(fresh == 0) return ans;
return -1;
}