-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathROTTEN_ORANGES_CPP.cpp
51 lines (51 loc) · 1.59 KB
/
ROTTEN_ORANGES_CPP.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
class Solution {
public:
int orangesRotting(vector<vector<int>>& grid) {
int row = grid.size();
int col = grid[0].size(), ans=0;
queue<pair<int, pair<int, int> > > q;
for(int i=0;i<row;i++){
for (int j=0;j<grid[i].size();j++){
if(grid[i][j] == 2){
q.push(make_pair(0,make_pair(i,j)));
}
}
}
while(!q.empty()){
int x = ((q.front()).second).first;
int y = ((q.front()).second).second;
int level = (q.front()).first;
q.pop();
// cout<<x<<" "<<y<<" "<<level<<'\n';
//bottom
if(x+1 < row && grid[x+1][y] == 1){
grid[x+1][y] = 2;
q.push(make_pair(level+1, make_pair(x+1, y)));
}
//top
if(x-1 >= 0 && grid[x-1][y] == 1){
grid[x-1][y] = 2;
q.push(make_pair(level+1, make_pair(x-1, y)));
}
//right
if(y+1 < col && grid[x][y+1] == 1){
grid[x][y+1] = 2;
q.push(make_pair(level+1, make_pair(x, y+1)));
}
//left
if(y-1 >= 0 && grid[x][y-1] == 1){
grid[x][y-1] = 2;
q.push(make_pair(level+1, make_pair(x, y-1)));
}
ans = max(ans, level);
}
for(int i=0;i<row;i++){
for (int j=0;j<grid[i].size();j++){
if(grid[i][j] == 1){
return -1;
}
}
}
return ans;
}
};