-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotting-oranges.cpp
27 lines (26 loc) · 1.1 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
class Solution {
public:
int orangesRotting(vector<vector<int>>& grid) {
queue<pair<int, int>> rotten;
int r = grid.size(), c = grid[0].size(), fresh = 0, t = 0;
for(int i = 0; i < r; ++i){
for(int j = 0; j < c; ++j){
if(grid[i][j] == 2) rotten.push({i, j});
else if(grid[i][j] == 1) fresh++;
}
}
while(!rotten.empty()){
int num = rotten.size();
for(int i = 0; i < num; ++i){
int x = rotten.front().first, y = rotten.front().second;
rotten.pop();
if(x > 0 && grid[x-1][y] == 1){ grid[x-1][y] = 2; fresh--; rotten.push({x-1, y});};
if(y > 0 && grid[x][y-1] == 1){ grid[x][y-1] = 2; fresh--; rotten.push({x, y-1});};
if(x < r-1 && grid[x+1][y] == 1){ grid[x+1][y] = 2; fresh--; rotten.push({x+1, y});};
if(y < c-1 && grid[x][y+1] == 1){ grid[x][y+1] = 2; fresh--; rotten.push({x, y+1});};
}
if(!rotten.empty()) t++;
}
return (fresh == 0) ? t : -1;
}
};