title: Find path in grid if possible using Recursion
body: <p>Given a grid of size R X C and startPosition and endPosition of a person in a grid composed of zero and one. Now I want to find path from start position to end position, and also trace the marked path on grid by labelling them 2. If path is not possible, I need to tell that path is not possible. So I had written this logic :</p>
  
  <pre><code>vector&lt;vector&lt;int&gt;&gt; solution;
  void printSolution(vector&lt;vector&lt;int&gt;&gt; grid)
  {
  for (int i=0;i&lt;grid.size();i++)
  {
      for (int j=0;j&lt;grid[i].size();j++)
          cout&lt;&lt;grid[i][j]&lt;&lt;" ";
      cout&lt;&lt;endl;
  }
  }
  
  bool isSafe(vector&lt;vector&lt;int&gt;&gt; grid, pair&lt;int,int&gt; position)
  {
  if(position.first &gt;= 0 &amp;&amp; position.first &lt; grid.size() &amp;&amp;
     position.second &gt;= 0 &amp;&amp; position.second &lt; grid[0].size() &amp;&amp;
     grid[position.first][position.second] == 0)
      return true;
  
  return false;
  }
  
  bool checkPath(vector&lt;vector&lt;int&gt;&gt; grid,pair&lt;int,int&gt; currentPosition,pair&lt;int,int&gt; endPosition){
  if(currentPosition == endPosition)
  {
      solution[currentPosition.first][currentPosition.second] = 2;
      return true;
  }
  if(isSafe(grid,currentPosition) == true)
  {
      solution[currentPosition.first][currentPosition.second] = 2;
      if (checkPath(grid, make_pair(currentPosition.first+1,currentPosition.second),endPosition) == true)
          return true;
      if (checkPath(grid, make_pair(currentPosition.first-1,currentPosition.second),endPosition) == true)
          return true;
      if (checkPath(grid, make_pair(currentPosition.first,currentPosition.second+1),endPosition) == true)
          return true;
      if (checkPath(grid, make_pair(currentPosition.first,currentPosition.second-1),endPosition) == true)
          return true;
      solution[currentPosition.first][currentPosition.second] = 0;
      return false;
  }
  
  return false;
  }
  
  bool solver(vector&lt;vector&lt;int&gt;&gt; grid,pair&lt;int,int&gt; startPosition,pair&lt;int,int&gt; endPosition,int R,int C){
  solution.resize(R,vector&lt;int&gt;(C));
  bool isPath = checkPath(grid,startPosition,endPosition);
  printSolution(solution);
  if(isPath==false){
      cout&lt;&lt;"No Path Found"&lt;&lt;endl;
      return false;
  }
  else{
      cout&lt;&lt;"Path Found"&lt;&lt;endl;
      return true;
  }
  }
  </code></pre>
  
  <p>This code is giving segmentation error. Please help to find it. I am stuck for almost a whole day to find where it is present.</p>
  
  <p>So help me correct the logic of code. Assume I have following fields :</p>
  
  <pre><code>int R,C;
  int grid[R][C];
  pair&lt;int,int&gt; startPosition;
  pair&lt;int,int&gt; endPosition;
  </code></pre>
  
  <p>This recursion is running infinite. i Checked for a simple case with startPosition as (1,0) and endPosition as (2,2) , R=3 and C=3 and grid as :</p>
  
  <pre><code>1 1 1
  0 0 1
  1 0 0
  </code></pre>
  
  <p>First, I tried with BFS, then start to make recursive solution.</p>