title: Can someone help to get Shortest Path with Obstacles?
body: <p>I have a 2D matrix.
  Given a 2D matrix where some of the elements are filled with '1' and the rest of the elements are filled with '0', except 2 elements, of which one is S (start point) and D (endpoint). Here '0' means you cannot traverse to that particular point. From a cell you can either traverse to left, right, up or down. Given two points in the matrix find the shortest path between these points.</p>
  
  <p>One of the shortest paths (from S to D both exclusive) is: [(3, 2), (3, 1), (2, 1), (2, 0)]. Return null if there is no path between S and D.</p>
  
  <p>I have writtes a piece of code which returns the distance to reach from S to D, my method returns int but how to return as expected output?
  My code:</p>
  
  <hr>
  
  <pre><code>public class ShortestPath {
  
       public static void main(String args[])
          {
              char[][] matrix = {
                  {'S', '0', '1', '1'},
                  {'1', '1', '0', '1'},
                  {'0', '1', '1', '1'},
                  {'1', '0', 'D', '1'}
              };
  
              int path = pathExists(matrix);
  
             System.out.println(path);
          }
  
      private static int pathExists(char[][] matrix) {
  
          Node source = new Node(0, 0, 0);
          Queue&lt;Node&gt; queue = new LinkedList&lt;Node&gt;();
  
          queue.add(source);
  
          while(!queue.isEmpty()) {
              Node poped = queue.poll();
  
              if(matrix[poped.x][poped.y] == 'D' ) {
                  return poped.distanceFromSource;
              }
              else {
                  matrix[poped.x][poped.y]='0';
  
                  List&lt;Node&gt; neighbourList = addNeighbours(poped, matrix);
                  queue.addAll(neighbourList);
              }   
          }
          return -1;
      }
  
      private static List&lt;Node&gt; addNeighbours(Node poped, char[][] matrix) {
  
          List&lt;Node&gt; list = new LinkedList&lt;Node&gt;();
  
          if((poped.x-1 &gt; 0 &amp;&amp; poped.x-1 &lt; matrix.length) &amp;&amp; matrix[poped.x-1][poped.y] != '0') {
              list.add(new Node(poped.x-1, poped.y, poped.distanceFromSource+1));
          }
          if((poped.x+1 &gt; 0 &amp;&amp; poped.x+1 &lt; matrix.length) &amp;&amp; matrix[poped.x+1][poped.y] != '0') {
              list.add(new Node(poped.x+1, poped.y, poped.distanceFromSource+1));
          }
          if((poped.y-1 &gt; 0 &amp;&amp; poped.y-1 &lt; matrix.length) &amp;&amp; matrix[poped.x][poped.y-1] != '0') {
              list.add(new Node(poped.x, poped.y-1, poped.distanceFromSource+1));
          }
          if((poped.y+1 &gt; 0 &amp;&amp; poped.y+1 &lt; matrix.length) &amp;&amp; matrix[poped.x][poped.y+1] != '0') {
              list.add(new Node(poped.x, poped.y+1, poped.distanceFromSource+1));
          }       
          return list;
      }
  }
  class Node {
      int x;
      int y;
      int distanceFromSource;
  
      Node(int x, int y, int dis) {
          this.x = x;
          this.y = y;
          this.distanceFromSource = dis;
      }
  }
  </code></pre>
  
  <hr>