Help me solve this You are currently located inside a maze. The walls of the maz
ID: 3595627 • Letter: H
Question
Help me solve this
You are currently located inside a maze. The walls of the maze are indicated by asterisks (*) k * Use the following recursive approach to check whether you can escape from the maze If you are at an exit, return true. Recursively check whether you can escape from one of the empty neighboring locations without visiting the current location This method merely tests whether there is a path out of the maze. Extra credit (+20 points) if you can print out a path that leads to an exit.Explanation / Answer
public boolean solve(int row, int col, int Maze[9][9], bool visited[9][9]) {
bool solved = false;
char right = Maze[row][col + 1];
char left = Maze[row][col - 1];
char up = Maze[row - 1][col];
char down = Maze[row + 1][col];
visited[row][col] = true;
if (right == 'G' || left == 'G' || up == 'G' || down == 'G') {
solved = true;
}
System.out.println("position=>"+"("+row + ":" + col+")");
if (right == ' ' && !visited[row][col+1]) {
solved = solve(row,col+1,Maze,visited);
}
if (down == ' ' && !visited[row+1][col]) {
solved = solve(row+1,col,Maze,visited);
}
if (left == ' ' && !visited[row][col-1]) {
solved = solve(row,col-1,Maze,visited);
}
if (up == ' ' && !visited[row-1][col]) {
solved = solve(row-1,col.Maze,visited);
}
return solved;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.