title: How do I find path using 2d array maze in java
body: <pre><code>B B B B B
  
  B B B O B
  
  B B S O B
  
  B O O B B
  
  B B X B B
  </code></pre>
  
  <p>Here,</p>
  
  <p>S = Start Point(2,2)</p>
  
  <p>B = Block</p>
  
  <p>O = Open</p>
  
  <p>X = Exit </p>
  
  <p>I want to make a maze that will check north, west, east and south. if X is around it will return the program. If not, then check for any 'O' around the start point and pass the new start point recursively. It there is no way to go and 'X' is not found it will go back to the original start point(2,2) and check west, east and south.</p>
  
  <p>after the program, i got:</p>
  
  <pre><code>B B B B B
  
  B B B + B
  
  B B + + B
  
  B O + B B
  
  B B X B B
  </code></pre>
  
  <p>but, I expect the output after the recursion is:</p>
  
  <pre><code>B B B B B
  
  B B B - B
  
  B B + - B
  
  B O + B B
  
  B B X B B
  </code></pre>
  
  <p>This is my code right now:</p>
  
  <pre><code>public static void Find_Path(char[][] maze, int startX, int startY){
  int x[] = { -1, 0, 0, 1};
  int y[] = {0,-1,1,0};
  boolean found = false;
  maze[startX][startY] = '+';
  for(int i = 0; i &lt; 4 ; i++){// check if there are 'X' around the S.
    int afterX = x[i] + startX;
    int afterY = y[i] + startY;
    if(maze[afterX][afterY] == 'X'){// if yesy, return.
      found = true;
      return;
    }
  }
  
  for(int i = 0; i &lt; 4 ; i++){// if no, check for 'O'
    int afterX = x[i] + startX;
    int afterY = y[i] + startY;
    if(maze[afterX][afterY] == 'O'){
      Find_Path(maze, afterX, afterY);
      if(!found){
       maze[afterX][afterY] = '-';
    }
   }
  }
  </code></pre>
  
  <p>startX and startY is the index of the start point.</p>
  
  <p>I don't know how to turn the wrong path '-'. The program will check north first If top is B then it will backtrack and go back to the start point. Then, it will go east, west and south.</p>
  
  <p>Can anyone help me out?? thx!
  @Muntasir</p>
  
  <pre><code>BBBBB
  BBOOB
  XOBOB
  BSOBB
  BBBBB
  
  BBBBB
  BBOOB
  X+BOB ( It should stop right here, because there is X around it.)
  BSOBB
  BBBBB
  
  BBBBB
  BBOOB
  X+BOB (but, somehow it go to the right of the startpoint and mark the 'O' to  '+'
  BS+BB
  BBBBB
  </code></pre>