-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrotten_oranges.cpp
More file actions
69 lines (63 loc) · 1022 Bytes
/
Copy pathrotten_oranges.cpp
File metadata and controls
69 lines (63 loc) · 1022 Bytes
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
65
66
67
#include<iostream>
#include<queue>
using namespace std;
struct elem{
int x;
int y;
};
bool safe(int x,int y, int R,int C)
{
if(x>=0 && x<R && y>=0 && y<C)
return true;
return false;
}
void ro(int g[][5],int R,int C)
{
int cycle = 0;
int row[] = {0,-1,1,0};
int col[] = {-1,0,0,1};
queue<elem>q;
// push all rotten oranges
for(int i=0;i<R;i++)
{
for(int j=0;j<C;j++)
{
if(g[i][j] == 2)//rotten
q.push(elem{i,j});
}
}
// push delim
q.push(elem{-1,-1});
while(!q.empty())
{
elem e = q.front();
if(e.x == -1 && e.y == -1)
{
q.pop();
if(q.empty())
break;
q.push(elem{-1,-1});
cycle++;
continue;
}
q.pop();
int x = e.x, y = e.y;
for(int i=0;i<4;i++)
{
if(safe(x+row[i],y+col[i],R,C) && g[x+row[i]][y+col[i]] == 1)// valid pos and fresh orange
{
g[x+row[i]][y+col[i]] = 2;
cout<<"\nPushing: "<<x+row[i]<<" "<<y+col[i];
q.push(elem{x+row[i],y+col[i]});
}
}
}
cout<<"\nCyles: "<<cycle;
}
main()
{
int arr[][5] = { {2, 1, 0, 2, 1},
{1, 0, 1, 2, 1},
{1, 0, 0, 2, 1}};
ro(arr,3,5);
}