title: the path to a point in a matrix. Want to solve it by DP
body: <p>The given matrix is the problem matrix. With cell 0,0 marked as 2 to show the entry point. We have to find the path to reach element 9. I have used recursive backtracking and marked the valid path as 2 that will lead to the element 9. Can someone help me in implementing it using DP. </p>
  
  <pre><code>include &lt;stdio.h&gt;
  int path(int [][7],int ,int);
  static int exitstack=0;
  int main(void) {
      int result,i,j;
      int arr[7][7]={
      {2,1,1,1,1,1,1},
      {0,1,1,0,0,0,1},
      {0,1,0,1,1,0,1},
      {1,0,9,0,1,1,1},      //the problem matrix
      {1,0,1,0,1,1,0},
      {1,1,1,1,1,0,1},
      {1,0,0,0,0,0,1}
      };
      path(arr,0,0);
  
      for(i=0;i&lt;7;++i)
      {
          for(j=0;j&lt;7;++j)
              printf("%d",arr[i][j]);
  
          printf("\n");
  
      }
  
      return 0;
  }
  
  int path(int p[7][7],int x,int y)
  {
      int i;
      if(p[x][y]==9)
      {
          exitstack=1;
          return 1;
      }
  
      for(i=1;i&lt;=4;++i)
      {
  
          if(i==1) //moving right
          {
  
  
              if((y+1)&lt;7)
              {
                  if(p[x][(y+1)]==9)
                      path(p,x,(y+1));
  
                  if(p[x][(y+1)]==1)
                  {
                      p[x][(y+1)]=2;
                      if(path(p,x,(y+1))) 
                          return 1;
                  }
              }
          }
          if(i==2) //moving up
          {
  
              if((x-1)&gt;0)
              {
                  if(p[(x-1)][y]==9)
                      path(p,x-1,y);
  
                  if(p[(x-1)][y]==1)
                  {
                      p[x-1][y]=2;
                      if(path(p,x-1,y))
                          return 1;
  
                  }
  
              }   
          }
  
  
          if(i==3) //moving left
          {
  
              if(y-1&gt;0)
              {
                  if(p[x][(y-1)]==9)
                      path(p,x,y-1);
  
                  if(p[x][(y-1)]==1)
                  {
                      p[x][(y-1)]=2;
                      if(path(p,x,y-1))
                          return 1;
  
                  }
              }
          }
          if(i==4) //moving down
          {
  
              if(x+1&lt;7)
              {
                  if(p[x+1][y]==9)
                      path(p,x+1,y);
  
                  if(p[x+1][y]==1)
                  {
                      p[x+1][y]=2;
                      if(path(p,x+1,y))
                          return 1;
                  }
              }
          }
      }   
  
      if(p[x][y]==9)
      {
  
          return 1;
      }
      else
      {
          if(exitstack==1)
              return 1;
          else
          {
              p[x][y]=0;  
              return 0;
          }
      }
  
  }
  </code></pre>