title: Depth First Search on 2-D array
body: <p>I am trying to learn DFS by creating a program that navigates my ogre through a maze (2d array).This is similar to a dailyprogramming challenge, but I am doing it with just a 1x1 ogre.</p>
  
  <p>My maze:</p>
  
  <pre><code>static int[][] maze = { 
  {2,1,0,0,0,0,0,0,0,0},
  {0,0,1,0,0,0,0,0,0,0},
  {1,0,0,0,0,1,0,1,0,0},
  {0,0,0,0,0,0,0,0,0,0},
  {0,0,1,1,0,0,0,0,0,0},
  {0,0,1,0,0,0,0,1,0,1},
  {1,1,0,0,0,0,0,0,0,0},
  {0,0,0,0,0,0,0,0,0,0},
  {0,0,0,0,0,1,1,0,0,0},
  {0,0,0,0,0,1,0,0,0,3}};
  </code></pre>
  
  <p>Where 2 is my hero (0,0), 3 is my goal (9,9), 1s are obstacles, and 0s are traverseable space.</p>
  
  <p>Since I am new to this, I doubt it will be needed, but ill include the whole program for easy duplication and troubleshooting.</p>
  
  <pre><code>import java.awt.Point;
  import java.util.ArrayList;
  
  
  public class OgrePath {
  
      static int[][] maze = { 
          {2,1,0,0,0,0,0,0,0,0},
          {0,0,1,0,0,0,0,0,0,0},
          {1,0,0,0,0,1,0,1,0,0},
          {0,0,0,0,0,0,0,0,0,0},
          {0,0,1,1,0,0,0,0,0,0},
          {0,0,1,0,0,0,0,1,0,1},
          {1,1,0,0,0,0,0,0,0,0},
          {0,0,0,0,0,0,0,0,0,0},
          {0,0,0,0,0,1,1,0,0,0},
          {0,0,0,0,0,1,0,0,0,3}};
  public static boolean[][] visited = new boolean[maze.length][maze[0].length];
  static ArrayList&lt;Point&gt; neighbors = new ArrayList&lt;Point&gt;();
  
  public static void main(String[] args) {
      OgrePath OP = new OgrePath();
      for (int i=0;i&lt;maze.length;i++){
          for (int j=0;j&lt;maze[i].length;j++){
              visited[j][i] = false;
          }
      }
      visited[getOgre(maze).x][getOgre(maze).y] = true;
      System.out.println("Ogre: " + getOgre(maze));
      dfs(maze, getOgre(maze));
  }
  
  public static boolean dfs(int[][] maze, Point p){
      neighbors = getNeighbors(maze,p);
      if (maze[p.x][p.y] == 3){
          System.out.println("FOUND IT");
          return true;
      }
      if (neighbors.isEmpty()){
          return false;
      }
      for (int i=0;i&lt;neighbors.size();i++){
          System.out.println("Nieghbors: " + neighbors);
          System.out.println(i + "(" + p.x + "," + p.y + ")");
          visited[neighbors.get(i).x][neighbors.get(i).y] = true;
          dfs(maze, neighbors.get(i));
      }
      return false;
  }
  
  public static ArrayList&lt;Point&gt; getNeighbors(int[][] maze, Point p){
      ArrayList&lt;Point&gt; neighbors = new ArrayList&lt;Point&gt;();
      Point left = new Point();
      Point right = new Point();
      Point down = new Point();
      Point up = new Point();
      down.x = p.x - 1;
      down.y = p.y;
      if (valid(maze,down)) neighbors.add(down);
      up.x = p.x + 1;
      up.y = p.y;
      if (valid(maze,up)) neighbors.add(up);
      left.x = p.x;
      left.y = p.y - 1;
      if (valid(maze,left)) neighbors.add(left);
      right.x = p.x;
      right.y = p.y + 1;
      if (valid(maze,right)) neighbors.add(right);
      return neighbors;
  }
  
  public static boolean valid(int[][] maze, Point p){
      if (inMaze(maze,p) &amp;&amp; canGo(maze,p) &amp;&amp; visited[p.x][p.y] == false) return true;
      else return false;
  }
  
  public static boolean inMaze(int[][] maze, Point p){
      if (p.x &lt; (maze[0].length - 1) &amp;&amp; p.x &gt; -1 &amp;&amp; p.y &lt; (maze.length - 1) &amp;&amp; p.y &gt; -1){
          return true;
      } else return false;
  }
  
  public static boolean canGo(int[][] maze, Point p){
      if (maze[p.x][p.y] != 1 &amp;&amp; maze[p.x][p.y] != 4) return true;
      else return false;  
  }
  
  public static Point getOgre(int[][] maze){
      Point ogre = new Point();
      for (int i=0;i&lt;maze.length;i++){
          for (int j=0;j&lt;maze[i].length;j++){
              if (maze[i][j] == 2){
                  ogre.x = j;
                  ogre.y = i;
              }
          }
      }
      return ogre;
  }
  }
  </code></pre>
  
  <p>I want to be able to recursively call DFS, but something about the way I wrote it makes the program stop after it has explored 1 possible line and failed.</p>