title: Shortest source to destination path
body: <p>I am trying to find the minimum number of steps required to reach to the destination from the source. For this, I am using BFS algorithm of graph traversal. I think I have applied the correct logic for BFS algorithm but I am not getting the correct answer when the destination node cannot be reached from source. Can, anyone check what I have done wrong in the code.</p>
  <pre><code>int shortestDistance(int N, int M, vector&lt;vector&lt;int&gt;&gt; A, int X, int Y) {
          
          vector&lt;vector&lt;int&gt;&gt;visited(N, vector&lt;int&gt;(M, 0));
          
          visited[0][0] = 1;
          queue&lt;pair&lt;int, int&gt;&gt;qe;
          pair&lt;int, int&gt;pr;
          pr.first = 0;
          pr.second = 0;
          
          qe.push(pr);
          queue&lt;int&gt;count;
          count.push(0);
          
          while(!qe.empty()){
              pair&lt;int, int&gt;pr1 = qe.front();
              qe.pop();
              
              int a = pr1.first;
              int b = pr1.second;
              int cnt = count.front();
              count.pop();
              if(a == X &amp;&amp; b == Y){
                  return cnt;
              }
              
              if(a-1 &gt;= 0 &amp;&amp; A[a][b] == 1 &amp;&amp; visited[a-1][b] == 0){
                  visited[a-1][b] = 1;
                  pair&lt;int, int&gt;pr2;
                  pr2.first = a-1;
                  pr2.second = b;
                  qe.push(pr2);
                  
                  count.push(cnt+1);
              }
              
              if(a+1 &lt; N &amp;&amp; A[a][b] == 1 &amp;&amp; visited[a+1][b] == 0){
                  visited[a+1][b] = 1;
                  pair&lt;int, int&gt;pr2;
                  pr2.first = a+1;
                  pr2.second = b;
                  qe.push(pr2);
                  count.push(cnt+1);
              }
              
              
              if(b-1 &gt;= 0 &amp;&amp; A[a][b] == 1 &amp;&amp; visited[a][b-1] == 0){
                  visited[a][b-1] = 1;
                  pair&lt;int, int&gt;pr2;
                  pr2.first = a;
                  pr2.second = b-1;
                  qe.push(pr2);
                  count.push(cnt+1);
              }
              
              if(b+1 &lt; M &amp;&amp; A[a][b] == 1 &amp;&amp; visited[a][b+1] == 0){
                  visited[a][b+1] = 1;
                  pair&lt;int, int&gt;pr2;
                  pr2.first = a;
                  pr2.second = b+1;
                  qe.push(pr2);
                  count.push(cnt+1);
              }
          }
          
          return -1;
      }
  }
  </code></pre>