-
Notifications
You must be signed in to change notification settings - Fork 6
/
0994.cpp
55 lines (42 loc) · 1.43 KB
/
0994.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 Solution {
public:
int orangesRotting(vector<vector<int>>& grid) {
int row = grid.size();
int col = grid[0].size();
vector<pair<int, int>> dir = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}};
queue<pair<int, int>> rotten;
int countFresh = 0;
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
if(grid[i][j] == 1)
countFresh++;
else if(grid[i][j] == 2)
rotten.push({i, j});
}
}
int timer = -1;
while(!rotten.empty()){
int siz = rotten.size();
for(int i = 0; i < siz; i++){
auto frnt = rotten.front();
rotten.pop();
for(auto d : dir){
int x = frnt.first + d.first;
int y = frnt.second + d.second;
if(x >= 0 && y >= 0 && x < row && y < col && grid[x][y] == 1){
grid[x][y] = 2;
rotten.push({x, y});
countFresh--;
}
}
}
timer++;
}
if(countFresh > 0)
return -1;
else if(timer == -1)
return 0;
else
return timer;
}
};