Search code examples
javarecursionflood-fill

How do I find path using 2d array maze in java


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

Here,

S = Start Point(2,2)

B = Block

O = Open

X = Exit

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.

after the program, i got:

B B B B B

B B B + B

B B + + B

B O + B B

B B X B B

but, I expect the output after the recursion is:

B B B B B

B B B - B

B B + - B

B O + B B

B B X B B

This is my code right now:

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 < 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 < 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] = '-';
  }
 }
}

startX and startY is the index of the start point.

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.

Can anyone help me out?? thx! @Muntasir

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

Solution

  • Use a global variable (a flag) to determine whether you found the correct path.

    For example:

    public class YourClass{
        static boolean found = false; // the global flag
        // your existing code
    
        public static void Find_Path(char[][] maze, int startX, int startY){
            // ....
            for(int i = 0; i < 4 ; i++){
                // ...
                if(maze[afterX][afterY] == 'X'){
                    found = true; // path found
                    return;
                }
            }
            for(int i = 0; i < 4 ; i++){
                // ...
                if(found) // path already found in earlier recursive call; no need to search anymore
                    return;
                else{ // path not found yet, have to continue searching
                    if(maze[afterX][afterY] == 'O'){
                        Find_Path(maze, afterX, afterY);
                        if(!found){ // path not found
                            maze[afterX][afterY] = '-';
                        }
                    }
                }
            }
        }
    }