Question: whats wrong with this code import java.io . * ; import java.util. * ; public class Main { / / Recursive function to traverse the

whats wrong with this code "import java.io.*;
import java.util.*;
public class Main {
// Recursive function to traverse the maze
public static boolean traverseMaze(char[][] maze, int x, int y, int exitRow, int exitCol, List path){
// Check boundaries
if (x <0|| x >= maze.length || y <0|| y >= maze[0].length){
return false; // Out of bounds
}
// If it's a wall or already visited, return false
if (maze[x][y]=='X'|| maze[x][y]=='+'|| maze[x][y]=='.'){
return false;
}
// Print the current position
System.out.println("Visiting: ("+ x +","+ y +")");
// If we reached the exit, mark it and return true
if (x == exitRow && y == exitCol){
maze[x][y]='+'; // Mark the exit point
path.add(new int[]{x, y}); // Add the exit to the path
return true;
}
// Mark the current cell as part of the path
maze[x][y]='+';
path.add(new int[]{x, y}); // Add this cell to the path
// Explore in all four directions: down, right, up, left
if (traverseMaze(maze, x +1, y, exitRow, exitCol, path)||// Move down
traverseMaze(maze, x, y +1, exitRow, exitCol, path)||// Move right
traverseMaze(maze, x -1, y, exitRow, exitCol, path)||// Move up
traverseMaze(maze, x, y -1, exitRow, exitCol, path)){// Move left
return true;
}
// Mark the cell as part of a dead end and backtrack
maze[x][y]='.';
path.remove(path.size()-1); // Remove this cell from the path
return false;
}
// Function to load the maze from a file, skipping the first line with row and column count
public static char[][] loadMazeFromFile(String filePath, int[] startPos, int[] endPos) throws IOException {
List mazeList = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))){
String firstLine = reader.readLine(); // Read the first line (row and column count)
String[] dimensions = firstLine.split("");
int rows = Integer.parseInt(dimensions[0]);
int cols = Integer.parseInt(dimensions[1]);
// Reading the actual maze from the second line onward
String line;
int row =0;
while ((line = reader.readLine())!= null){
char[] mazeRow = line.toCharArray();
// Find start ('+') and exit ('-')
for (int col =0; col < mazeRow.length; col++){
if (mazeRow[col]=='+'){
startPos[0]= row;
startPos[1]= col;
} else if (mazeRow[col]=='-'){
endPos[0]= row;
endPos[1]= col;
}
}
mazeList.add(mazeRow);
row++;
}
// Check if loaded maze matches the expected dimensions
if (mazeList.size()!= rows || mazeList.get(0).length != cols){
throw new IllegalArgumentException("Maze dimensions do not match the provided size.");
}
}
return mazeList.toArray(new char[mazeList.size()][]);
}
public static void main(String[] args){
try {
// Define positions to hold start and end points
int[] startPos = new int[2];
int[] endPos = new int[2];
// Load the maze from the file
String filePath ="C:\\CS113\\m\\src\\maze"; // Path to your maze file
char[][] maze = loadMazeFromFile(filePath, startPos, endPos);
// Start position (determined from file as '+')
int startX = startPos[0];
int startY = startPos[1];
// Exit position (determined from file as '-')
int exitX = endPos[0];
int exitY = endPos[1];
// Print start and end coordinates for debugging
System.out.println("Start: ("+ startX +","+ startY +")");
System.out.println("Exit: ("+ exitX +","+ exitY +")");
// List to store the path
List path = new ArrayList<>();
if (traverseMaze(maze, startX, startY, exitX, exitY, path)){
System.out.println("Path found!");
} else {
System.out.println("No path exists.");
}
// Print the maze with the path and dead ends marked
for (char[] row : maze){
System.out.println(new String(row));
}
// Print the coordinates of the path
System.out.println("
Path Traversed:");
for (int[] coords : pa

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!