forked from GDG-IGDTUW/Interview-Preparation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.cpp
More file actions
64 lines (53 loc) · 1.69 KB
/
Copy pathsolution.cpp
File metadata and controls
64 lines (53 loc) · 1.69 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
54
55
56
57
58
59
60
61
62
63
64
class Solution {
public:
int orangesRotting(vector<vector<int>>& grid) {
int m=grid.size();
int n=grid[0].size();
vector<vector<bool>>vis(m,vector<bool>(n,false));
queue<pair<pair<int,int>,int>>q;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(grid[i][j]==2){
vis[i][j]=true;
q.push({{i,j},0});
}
}
}
int ans=0;
while(!q.empty()){
int row=q.front().first.first;
int col=q.front().first.second;
int time=q.front().second;
q.pop();
ans=max(ans,time);
if(row-1>=0 && !vis[row-1][col] && grid[row-1][col]==1){
grid[row-1][col]=2;
vis[row-1][col]=true;
q.push({{row-1,col},time+1});
}
if(row+1<m && !vis[row+1][col] && grid[row+1][col]==1){
grid[row+1][col]=2;
vis[row+1][col]=true;
q.push({{row+1,col},time+1});
}
if(col-1>=0 && !vis[row][col-1] && grid[row][col-1]==1){
grid[row][col-1]=2;
vis[row][col-1]=true;
q.push({{row,col-1},time+1});
}
if(col+1<n && !vis[row][col+1] && grid[row][col+1]==1){
grid[row][col+1]=2;
vis[row][col+1]=true;
q.push({{row,col+1},time+1});
}
}
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(grid[i][j]==1 && !vis[i][j]){
return -1;
}
}
}
return ans;
}
};