File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed
Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ int orangesRotting (vector<vector<int >>& grid) {
4+ int i,j,k,n,m,ct=0 ;
5+ n=grid.size ();
6+ m=grid[0 ].size ();
7+ int vis[n][m];
8+ memset (vis,-1 ,sizeof (vis));
9+ queue<tuple<int ,int ,int >> q;
10+ for (i=0 ;i<n;i++){
11+ for (j=0 ;j<m;j++){
12+ if (grid[i][j]==2 ) q.push ({i,j,0 });
13+ if (grid[i][j]==1 or grid[i][j]==2 ) ct++;
14+ }
15+ }
16+ int ans=0 ;
17+ while (q.empty ()==false ){
18+ auto [x,y,time] = q.front ();
19+ q.pop ();
20+ if (vis[x][y]==1 ) continue ;
21+ else vis[x][y]=1 ;
22+ ans = max (ans,time);
23+ ct--;
24+ if (x-1 >=0 ){
25+ if (grid[x-1 ][y]==1 ) q.push ({x-1 ,y,time+1 });
26+ }
27+ if (x+1 <n){
28+ if (grid[x+1 ][y]==1 ) q.push ({x+1 ,y,time+1 });
29+ }
30+ if (y+1 <m){
31+ if (grid[x][y+1 ]==1 ) q.push ({x,y+1 ,time+1 });
32+ }
33+ if (y-1 >=0 ){
34+ if (grid[x][y-1 ]==1 ) q.push ({x,y-1 ,time+1 });
35+ }
36+ }
37+ if (ct!=0 ) return -1 ;
38+ return ans;
39+ }
40+ };
You can’t perform that action at this time.
0 commit comments