Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursive Maze Algorithm

I'm solving a maze using recursion. my matrix look like this

char maze[][] = 
    {
        {'#','#','#','#','#','#','#','#','#','#','#'},
        {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
        {'#','#','#','#',' ','#','#','#','#','#','#'},
        {'#',' ',' ',' ',' ','#',' ',' ',' ',' ','X'},
        {'#',' ',' ',' ',' ','#',' ',' ',' ',' ','#'},
        {'#',' ','#','#','#','#','#','#',' ','#','#'},
        {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
        {'#','#','#','#','#','#','#','#','#','#','#'},

    };

this is prototype of the bigger matrix. my solve recursion method looks like this

private void solve(int x, int y) {
    // set everything to false
    wasHere = new boolean[maze.length][maze[1].length];
    correctPath = new boolean[maze.length][maze[1].length];
     for (int i = 0; i < maze.length; i++){  
            // Sets boolean Arrays to default values
            for (int j = 0; j < maze[i].length; j++){
                wasHere[i][j] = false;              // set everything to false
                correctPath[i][j] = false;          
            }
     }
boolean b = solveRecursion(1,1);
System.out.println(b);

}

as you can notice here I'm returning a boolean and it should give me a true if I find a path. But it is keep giving me false. I'm not sure about the logical errors that I'm making in my solveRecursion method. Here's the method

    private boolean solveRecursion(int x, int y) {
        if (x == endX && y == endY){
            return true;
        }
        if (maze[x][y] == '#' || wasHere[x][y]){
            return false;
        }
        wasHere[x][y]=true;
        if (x != 0) // Checks if not on left edge
            if (solveRecursion(x-1, y)) { // Recalls method one to the left
                correctPath[x][y] = true; // Sets that path value to true;
                maze[x][y]='S';
                return true;
            }
        if (x != maze[0].length ) // Checks if not on right edge
                if (solveRecursion(x+1, y)) { // Recalls method one to the right
                    correctPath[x][y] = true;
                    maze[x][y]='S';
                    return true;
            }
        if (y != 0)  // Checks if not on top edge
            if (solveRecursion(x, y-1)) { // Recalls method one up
                correctPath[x][y] = true;
                maze[x][y]='S';
                return true;
           }
        if (y != maze.length) // Checks if not on bottom edge
            if (solveRecursion(x, y+1)) { // Recalls method one down
                correctPath[x][y] = true;
                maze[x][y]='S';
                return true;
            }
        return false;


}

endX =3; endY =10;

like image 933
Shadid Avatar asked Oct 20 '22 12:10

Shadid


1 Answers

You are confusing yourself with the coordinate system of your grid. In your case, X represents the number of rows while Y represents the columns.

You are checking to see if (y != maze.length) and if so you move "one down" which in reality is moving over to the right one, not down one.

This confusion has your Y coordinate limited by your X values.

You should change that one line to:

 if (y != maze[0].length)

That would fix the problem. As it stands you never reach the value (3,9) which is just 1 away from the end, because your if statement checks the coordinates (8,9) and says:

 if (y != maze.length) // Hmm. my coordinates are (8,9) so y = 9. 
                       // maze.length is 9, so this is false.

And the edge values are never considered.

like image 158
leigero Avatar answered Oct 23 '22 01:10

leigero