-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrotting_oranges.cpp
More file actions
53 lines (43 loc) · 1.28 KB
/
rotting_oranges.cpp
File metadata and controls
53 lines (43 loc) · 1.28 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
49
50
51
52
53
class Solution {
public:
int orangesRotting(vector<vector<int>>& grid) {
int m = grid.size();
int n = grid[0].size();
int fresh = 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});
else if(grid[i][j] == 1)
fresh++;
}
}
if(fresh == 0)
return 0;
const int dr[4] = {-1, 1, 0, 0};
const int dc[4] = {0, 0, -1, 1};
int min = 0;
while(!q.empty()){
int len = q.size();
if(fresh == 0)
return min;
for(int i = 0; i < len; i++){
int cr = q.front().first;
int cc = q.front().second;
q.pop();
for(int j = 0; j < 4; j++){
int nr = cr + dr[j];
int nc = cc + dc[j];
if(nr < 0 || nr >= m || nc < 0 || nc >= n || grid[nr][nc] != 1)
continue;
grid[nr][nc] = 2;
fresh--;
q.push({nr, nc});
}
}
min++;
}
return -1;
}
};