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

Secret.BoBo
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
Muntasir

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I find shortest path in maze with BFS?

Java Maze shortest path 2d int array

How do I find the solution of a particular maze using recursive bactracking (Java)?

How do I find the middle of a 2d array?

How do I create a table using 2D array in Java?

How do i convert a Set into 2D array in java?

How do I insert a row into a 2D array in java?

How do I create a 2d array made of 2d int arrays in java?

How do I output the quickest path of a maze via backtracking?

How to find the shortest path in 2D Array with multiple endpoints?

How do I use a loop to multiply a row of a 2D array to the column of a second 2D array, and etc in Java?

How can I write a function to find 2 closest points in 2D dimensional array in Java?

FInd a path through a maze using recursion

Given a 2d array of Ints, how do I find the highest numbers and their positions?

Using HTMLEditorKit in Java, how do I find what local file path <img src=...> tags will use?

How do i sort a 2D array or multiple arrays by the length of the array using bubble sort

Solve 2D array maze using recursion

How do I do a deep copy of a 2d array in Java?

How to find shortest path in this type of maze

How to find shortest path in skeletonized maze image?

How do I make this maze?

How do I randomly move objects in a maze using pygame?

How do I find out my python path using python?

How do I find the path of a file using os.walk?

I want to call a Java method using a 2D array of values. How do I set up my for loop to call the method correctly?

How do I sort a 2d array of ints in Kotlin using sortedBy?

How do I obtain Kernel Density Estimator on a 2D image/array using sklearn?

How do I declare a 2d array in C++ using new?

How do I plot a 2D array graph in Python using matplotlib