Menu

Updated Include Direction Move Prerequisites Scenario Maze H W Rectangle Represented Array Q43816968

*I updated to include which direction we can move after theprerequisites.

Scenario

Our maze is an H by W rectangle, representedby an array of size H of W-sized strings. Eachcharacter in a string can either be ‘#’ or ‘.’. ‘#’ represents awall, which we cannot cross, and ‘.’ represents a free space, whichwe can cross. The border of the maze is always filled with ‘#’except for one square, which represents the exit. For example, thefollowing is a valid maze:

并##.## #... #,#, #,# , # #.###.# #,,,,,并 #######

Find the total number of steps to exit the maze, when suppliedwith a starting point (i,j) (with (0, 0) being theupper-left point and (H, W) being the lower-right point).

Aim: Use BFS to find the shortest path out of a given maze.

Prerequisites: Implement the distToExit() method of the Mazeclass in the source code, which returns the integer distance fromthat point to the exit of the maze.

Assume that the points supplied to distToExit() are valid (thatis, they’re not inside walls). Remember that we can only move inthe cardinal directions (North, South, East, and West).

public int distToExit(int i, int j) { return -1;}

Steps for Completion

  1. Encode the maze representation to a graph representation.
  2. Apply the BFS implementation shown in the preceding lesson(with a small modification to account for distances), or you canbuild the graph as you go.
  3. Since you know that there are at most four outward edges for agiven vertex, compute their positions as you go.

Grading

Complete each task listed below. Each task contains automatedchecks which are used to calculate your grade. When you havecompleted each task by clicking the checkbox, open the task listpanel on the left navigation bar and click the “Submit” button.

Task: Implemented the distToExit() method using BFS.

Here is the code we are given to complete in JAVA, there is asection towards the end where we are to write our code:

public class Maze {
private int H;
private int W;
private int exitI;
private int exitJ;
private String[] maze;

public Maze(String[] maze) {
this.maze = maze;
this.H = maze.length;
this.W = maze[0].length();
for (int i = 0; i < W; i++) {
if (maze[0].charAt(i) == ‘.’) {
this.exitI = 0;
this.exitJ = i;
return;
}
if (maze[H – 1].charAt(i) == ‘.’) {
this.exitI = H – 1;
this.exitJ = i;
return;
}
}
for (int i = 1; i < H – 1; i++) {
if (maze[i].charAt(0) == ‘.’) {
this.exitI = i;
this.exitJ = 0;
return;
}
if (maze[i].charAt(W – 1) == ‘.’) {
this.exitI = i;
this.exitJ = W – 1;
return;
}
}
}

public int distToExit(int i, int j) {
// Write your code here
return -1;
}
}

并##.## #… #,#, #,# , # #.###.# #,,,,,并 ####### Show transcribed image text 并##.## #… #,#, #,# , # #.###.# #,,,,,并 #######

Expert Answer


Answer to *I updated to include which direction we can move after the prerequisites. Scenario Our maze is an H by W rectangle, rep…

OR