/**
 * solveMaze method finds whether a route to the exit
 * from startX, startY to E labeled in the maze
 *
 * @param maze   maze
 * @param startX starting x coordinate
 * @param startY starting y coordinate
 * @return true if there is a path to the exit
 */
public static boolean solveMaze(char[][] maze, int startX, int startY) {

    if (maze[startX][startY] == 'E') { // checks if current location is the exit
        return true; // exit path exists
    }

    maze[startX][startY] = '#'; // mark the current square to avoid revisiting
    if (startX != 0) { // checks for off-bound errors
        if (maze[startX - 1][startY] != '#') { // checks if there is a wall
            return solveMaze(maze, startX - 1, startY); // recurse with that coordinate
        }
    }
    if (startX != maze.length - 1) { // checks for off-bound errors
        if (maze[startX + 1][startY] != '#') { // checks if there is a wall
            return solveMaze(maze, startX + 1, startY); // recurse with that coordinate
        }
    }
    if (startY != maze[0].length - 1) { // checks for off-bound errors
        if (maze[startX][startY + 1] != '#') {
            return solveMaze(maze, startX, startY + 1); // recurse with that coordinate
        }
    }
    if (startY != 0) { // checks for off-bound errors
        if (maze[startX][startY - 1] != '#') { // checks if there is a wall
            return solveMaze(maze, startX, startY - 1); // recurse with that coordinate
        }
    }

    return false; // return false if on available path
}

// test case 1
char[][] maze1 = {
        {'#', '#', '#', '#', '#'},
        {'#', ' ', ' ', '#', 'E'},
        {'#', ' ', '#', ' ', '#'},
        {'#', ' ', ' ', ' ', '#'},
        {'#', '#', '#', '#', '#'}
};

char[][] maze2 = {
        {'#', '#', '#', '#', '#'},
        {'#', ' ', '#', '#', 'E'},
        {'#', ' ', '#', '#', '#'},
        {'#', ' ', ' ', ' ', '#'},
        {'#', '#', '#', '#', '#'}
};

char[][] maze3 = {
        {'#', '#', '#', '#', '#'},
        {'#', ' ', ' ', ' ', 'E'},
        {'#', ' ', '#', ' ', '#'},
        {'#', ' ', ' ', ' ', '#'},
        {'#', '#', '#', '#', '#'}
};


System.out.println(solveMaze(maze1, 1, 4)); // Output: true
System.out.println(solveMaze(maze2, 3, 1)); // Output: false
System.out.println(solveMaze(maze3, 1, 1)); // Output: true

true
false
true