title: Recursion with matrices - Rotten Oranges
body: <p>I am trying to solve the following problem:</p>
  
  <p>Given a matrix of dimensions <code>n*n</code> where each cell in the matrix can have values <code>0</code>, <code>1</code>, or <code>2</code> with the following meanings:</p>
  
  <pre><code>0:empty cell
  1:cells have fresh oranges
  2:cells have rotten oranges
  </code></pre>
  
  <p>Determine the minimum time required for all the oranges to be rotten. A rotten orange at index <code>[i,j]</code> can rot other fresh oranges at indexes <code>[i+1,j]</code>, <code>[i,j+1]</code>, <code>[i-1,j]</code>, and <code>[i,j-1]</code>.  If it is impossible to rot every orange then simply return -1;</p>
  
  <p>Below is my attempted solution</p>
  
  <pre><code>#include&lt;bits/stdc++.h&gt;
  using namespace std;
  void recur(int a[100][100],int t[100][100],int count,int i,int j,int n)
  {
      cout&lt;&lt;i&lt;&lt;" "&lt;&lt;j&lt;&lt;endl;
      if(i&lt;0||i&gt;=n||j&lt;0||j&gt;=n)//if out of boundary stop
      {
      //  cout&lt;&lt;"1\n";
          //return;
      }
      if(a[i][j]==1)  //if fresh orange then recur from here too
      {
      //  cout&lt;&lt;"2\n";
          t[i][j]=min(count,t[i][j]);//take this case 2 1 1 2...Third 1 can be rot in 2seconds as well as 1 second so I sotre minimum of both
  
          recur(a,t,count+1,i+1,j,n);//again recur in 4 directions
          recur(a,t,count+1,i,j+1,n);
          recur(a,t,count+1,i,j-1,n);
          recur(a,t,count+1,i-1,j,n);
  
      }
      else if(a[i][j]==2)//if already a rot orange no need to check in 4 directions as I am checking for every rot in loop in main()
      {
      /// cout&lt;&lt;"3\n";
          t[i][j]=0;//already rot so time required =0 seconds
      //  return;
      }
      else if(a[i][j]==0)//if empty space stop
      {
      //  cout&lt;&lt;"4\n";
      //  return;
          //cout&lt;&lt;"hi\n";
      }
  
  }
  main()
  {
      int a[100][100],t[100][100],n;//a is input array and t is array whihc stores time required to rot each fresh orange
      cin&gt;&gt;n;
      for(int i=0;i&lt;n;++i)
      {
          for(int j=0;j&lt;n;++j)
          {
              cin&gt;&gt;a[i][j];
              t[i][j]=INT_MAX;
          }
      }
      for(int i=0;i&lt;n;++i)
      {
          for(int j=0;j&lt;n;++j)
          {
              if(a[i][j]==2)
              {
                  recur(a,t,1,i+1,j,n);// recursion starts for top bottom left and right
                  recur(a,t,1,i,j+1,n);
                  recur(a,t,1,i,j-1,n);
                  recur(a,t,1,i-1,j,n);
  
              }
          }
      }
      int flag=0,val=0;
      for(int i=0;i&lt;n;++i)
      {
          for(int j=0;j&lt;n;++j)
          {
              if(a[i][j]==1&amp;&amp;t[i][j]==INT_MAX)
              {   
                  flag=-1;
                  break;
              }   
              else if(a[i][j]==1)
              val=max(t[i][j],val);
          }
      }
      if(flag==-1)
      val=-1;
  /*  else
      {
  
          for(int i=0;i&lt;n;++i)
          {
              for(int j=0;j&lt;n;++j)
              cout&lt;&lt;t[i][j]&lt;&lt;" ";
              cout&lt;&lt;endl;
          }
      }*/
      cout&lt;&lt;val;
  }
  </code></pre>
  
  <p>However, this recurs indefinitely.  Why?</p>