From 5669cf09f443d605ee561905fe363c3f2b57d395 Mon Sep 17 00:00:00 2001 From: optimistic-coder-001 <72167494+optimistic-coder-001@users.noreply.github.com> Date: Thu, 1 Oct 2020 02:24:59 +0530 Subject: [PATCH] Rotten_oranges_leetcode.cpp --- Graph/rotten_oranges.cpp | 129 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 Graph/rotten_oranges.cpp diff --git a/Graph/rotten_oranges.cpp b/Graph/rotten_oranges.cpp new file mode 100644 index 00000000..630b2d6b --- /dev/null +++ b/Graph/rotten_oranges.cpp @@ -0,0 +1,129 @@ +#include +#include +using namespace std; +int c; +int r; +void check(queue< pair > &q,int a[101][101],int &flag) +{ + pair b=q.front(); + int x=b.first; + int y=b.second; + + if(a[x-1][y]==1 && x>=0) + { + q.push({x-1,y}); + a[x-1][y]=2; + if(flag==0) + { + flag=1; + } + } + + if(a[x+1][y]==1 && x=0) + { + q.push({x,y-1}); + a[x][y-1]=2; + if(flag==0) + { + flag=1; + } + } + +} + + +int main() { + + ios_base:: sync_with_stdio(false); + cin.tie(NULL); cout.tie(NULL); + + // #ifndef ONLINE_JUDGE + // freopen("input.txt","r",stdin); + // freopen("output.txt","w",stdout); + // #endif + int t; + cin>>t; + for(int i=0;i>r; + cin>>c; + int a[101][101]; + queue< pair >q; + for(int i=0;i>a[i][j]; + + if(a[i][j]==2) + { + q.push({i,j}); + } + + } + + } + int time=0; + int flag=0; + q.push({-1,-1}); + while(!q.empty()) + { + if(q.front()!=make_pair(-1,-1)) + { + // we will create check_function + check(q,a,flag); + q.pop(); + } + else if(q.front()==make_pair(-1,-1)) + { + time=time+flag; + flag=0; + q.pop(); + if(q.empty()) + { + break; + } + else if(!q.empty()) + { + q.push({-1,-1}); + } + } + + } + + // now checking if no present 1 + for(int i=0;i