Java Question help needed In the program Fill the add state
Java Question : help needed In the program \"Fill the add statements\" here area. Sample Output in the picture
Consider a maze made up of a rectangular array of squares. The maze will contain a
character (either +, -, or |) to represent a blocked square, and to form the walls of the
maze. Mazes will have only one entrance at the Coordinate (0, 1), with only one exit in
the lower right hand corner of the maze.
Beginning at the entrance to the maze, find a path to the exit at the bottom right of the
maze. You may only move up, down, left, and right. Each square in the maze can be in
one of four states: clear (space), blocked (X), path (.), or visited (*). Initially, after the
maze has been read in from the file, each square will be either clear or blocked. If a
square lies on a successful path, mark it with a period. If you visit a square but it does
not lead to a successful path, mark it as visited with an asterisk.
This is the program for it
public class Coordinate {
public int x;
public int y;
public Coordinate( int x, int y ) {
this.x = x;
this.y = y;
}
public String toString() {
return \"(\" + this.x + \",\" + this.y + \")\";
}
@Override
public boolean equals( Object object ) {
if( object == null ) {
return false;
}
if( ! Coordinate.class.isAssignableFrom( object.getClass() )) {
return false;
}
final Coordinate other = (Coordinate) object;
return this.x == other.x && this.y == other.y;
}
}
import java.util.Vector;
public class Maze {
private char[][] maze;
private int height;
private int width;
/**
* Create a new Maze of the specified height and width, initializing every
* location as empty, with a \' \'.
**/
public Maze( int width, int height ) {
this.width = width;
this.height = height;
this.maze = new char [this.width][this.height];
// ADD STATEMENTS HERE
}
/**
* Mutator to allow us to set the specified Coordinate as blocked,
* marking it with a \'X\'
**/
public void setBlocked( Coordinate coord ) {
// ADD STATEMENTS HERE
}
/**
* Mutator to allow us to set the specified Coordinate as having been visited,
* marking it with a \'*\'
**/
public void setVisited( Coordinate coord ) {
// ADD STATEMENTS HERE
}
/**
* Mutator to allow us to set the specified Coordinate as part of the path solution,
* marking it with a \'.\'
**/
public void setPath( Coordinate coord ) {
// ADD STATEMENTS HERE
}
/**
* Returns the character at the locatio specified by the Coordinate
**/
public char at( Coordinate coord ) {
// ADD STATEMENTS HERE
}
/**
* Returns a Coordinate array containing all Coordinates that are clear around
* the specified coordinate.
**/
public Coordinate[] clearAround( Coordinate coord ) {
Vector vector = new Vector();
// ADD STATEMENTS HERE
// Look at each of the locations around the specified Coordinate, and add it
// to the vector if it is clear (i.e. a space)
return vector.toArray( new Coordinate[0] );
}
/**
* Returns a Coordinate that provides the entrance location in this maze.
**/
public Coordinate start() {
return new Coordinate( 0, 1 );
}
/**
* Returns a Coordinate that provides the exit location from this maze.
**/
public Coordinate end() {
// ADD STATEMENTS HERE
}
/**
* The toString() method is responsible for creating a String representation
* of the Maze. See the project specification for sample output. Note that
* the String representation adds numbers across the top and side of the Maze
* to show the Coordinates of each cell in the maze.
**/
public String toString() {
StringBuilder buffer = new StringBuilder();
// ADD STATEMENTS HERE
// First, print out the column headings
// Next, print out each row in the maze - note the spaces between
// cells to facilitate reading. Each row should include its row number.
for(int x=0; x
for(int y=0; y
buffer.append(maze[x][y]);
buffer.append(\"\ \");
return buffer.toString();
}
}
package p2;
import java.io.FileNotFoundException;
public class MazeSolver {
private Maze maze;
private LinkedStack path;
public MazeSolver( Maze maze ) {
// ADD STATEMENTS HERE
}
public void solve() {
// ADD STATEMENTS HERE
// Add the starting Coordinate to the maze, and while the Stack has
// entries, and the top of the Stack is not the end, continue searching
// for the path
}
public static void main( String[] args ) throws FileNotFoundException {
MazeReader reader = new MazeReader( \"sampleMaze.txt\" );
Maze maze = reader.open();
MazeSolver solver = new MazeSolver( maze );
System.out.println( \"Before solving\" );
System.out.println( maze );
System.out.println( \"Start is \" + maze.start() );
System.out.println( \"End is \" + maze.end() );
solver.solve();
System.out.println( \"After solving (. shows solution, o shows visited)\" );
System.out.println( maze );
}
}
Solution
Answer:
import java.io.*;
public class DetectedShortestPathInMaze
{
static int number_of_rows=10; static int number_of_columns=10;
static int begin_of_row=5; static int begin_of_coloumn=3;
static int end_of_row=1; static int end_of_coloumn=6;
static int taken_maze[][]={{1,1,1,1,1,1,1,1,1,1},
{1,0,0,1,0,0,0,1,0,1},
{1,0,1,1,1,0,1,1,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,1,0,1,1,0,1,1,1},
{1,0,0,0,0,1,0,0,0,1},
{1,0,1,1,1,0,0,1,1,1},
{1,0,1,1,1,1,0,1,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}};
static int shortestpath[]=new int[number_of_rows*number_of_columns];
static int length_short;
boolean visitedalready(int row, int col, int detectedpathsofar[], int detectedlengthsofar){
int x;
int goal = row*number_of_columns+col;
for (x=0;x<detectedlengthsofar;x++)
if (detectedpathsofar[x]==goal) return true;
return false;
}
public void displayupdatedpath(int takenpath[], int takenlength){
int r,c;
for (r=0;r<number_of_rows;r++){
for(c=0;c<number_of_columns;c++){
if (taken_maze[r][c]==1)
System.out.print(\"|\");
else if (r==begin_of_row && c==begin_of_coloumn)
System.out.print(\"S\");
else if (r==end_of_row && c==end_of_coloumn)
System.out.print(\"X\");
else if (visitedalready(r,c,takenpath,takenlength))
System.out.print(\"o\");
else
System.out.print(\" \");
}
System.out.println(\"\");
}
}
public void searchnewpath(int row, int col, int detectedpathsofar[], int detectedlengthsofar){
if (row<0 || col<0 || row>=number_of_rows || col>=number_of_columns)
return;
if (taken_maze[row][col]==1) return ;
if (visitedalready(row, col, detectedpathsofar, detectedlengthsofar)) return;
int takenpath[]=new int[detectedlengthsofar+1];
System.arraycopy(detectedpathsofar, 0, takenpath, 0, detectedlengthsofar);
takenpath[detectedlengthsofar++]=row*number_of_columns+col;
if (row==end_of_row && col==end_of_coloumn){
System.out.println(\"Detected path of length \"+detectedlengthsofar+\":\");
displayupdatedpath(takenpath, detectedlengthsofar);
if (detectedlengthsofar<=length_short){
length_short=detectedlengthsofar;
System.arraycopy(takenpath, 0, shortestpath, 0, detectedlengthsofar);
System.out.println(\" The new shortest path is of length \" + detectedlengthsofar);
}
System.out.println(\"\");
return;
}
searchnewpath(row-1, col, takenpath, detectedlengthsofar);
searchnewpath(row, col-1, takenpath, detectedlengthsofar);
searchnewpath(row, col+1, takenpath, detectedlengthsofar);
searchnewpath(row+1, col, takenpath, detectedlengthsofar);
}
public static void main(String[] args)
{
int r,c,x;
int detectedpathsofar[];
int detectedlengthsofar;
DetectedShortestPathInMaze obj=new DetectedShortestPathInMaze();
detectedpathsofar=new int[obj.number_of_rows*obj.number_of_columns];
for (x=0;x<obj.number_of_rows*obj.number_of_columns;x++){
obj.shortestpath[x]=-1;
detectedpathsofar[x]=-1;
}
obj.length_short=obj.number_of_rows*obj.number_of_columns+1;
detectedlengthsofar=0;
System.out.println(\"The Maze Is Shown As Below:\");
for (r=0;r<obj.number_of_rows;r++){
for (c=0;c<obj.number_of_columns;c++){
if (r==begin_of_row && c==begin_of_coloumn)
System.out.print(\"S\");
else if (r==end_of_row && c==end_of_coloumn)
System.out.print(\"x\");
else if (obj.taken_maze[r][c]==0)
System.out.print(\" \");
else System.out.print(\"|\");
}
System.out.println(\"\");
}
System.out.println(\"\");
System.out.println(\"Searching For Paths!!!!!\");
obj.searchnewpath(begin_of_row, begin_of_coloumn, detectedpathsofar, detectedlengthsofar);
System.out.println(\"\");
System.out.println(\"The shortest path was found with the following of length \"+ obj.length_short);
obj.displayupdatedpath(obj.shortestpath, obj.length_short);
}
}







