Java Question Consider a maze made up of a rectangular array
Java Question:
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.
Solution
import java.io.*;
public class DetectedShortestPathInMaze
{
int num_of_rows=10;
int num_of_cols=10;
int begin_of_row=5;
int begin_of_col=3;
int end_of_row=1; static int end_of_col=6;
int t_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}};
int spath[]=new int[num_of_rows*num_of_cols];
int len_short;
boolean val(int row, int col, int detpaths[], int detlen){
int k;
int goal = row*num_of_cols+coln;
for (k=0;k<detlen;k++)
if (detpath[x]==goal) return true;
return false;
}
public void disppath(int tpath[], int tlen){
int rw,cl;
for (rw=0;rw<num_of_rows;rw++){
for(cl=0;cl<num_of_cols;cl++){
if (t_maze[rw][cl]==1)
System.out.print(\"|\");
else if (rw==b_of_row && c==b_of_col)
System.out.print(\"S\");
else if (r==e_of_row && c==e_of_col)
System.out.print(\"X\");
else if (val(rw,cl,tpath,tlen))
System.out.print(\" 0 \");
else
System.out.print(\" * \");
}
System.out.println(\" . \");
}
}
public void snpath(int row, int coln, int dpath[], int dlens){
if (row<0 || coln<0 || row>=num_of_rows || cols>=num_of_cols)
return;
if (t_maze[row][coln]==1) return ;
if (val(row, coln, dpath, dlen)) return;
int tpath[]=new int[dlen+1];
System.arraycopy(dpath, 0, tpath, 0, dlen);
tpath[detlen++]=row*num_of_col+coln;
if (row==e_of_row && coln==e_of_col){
System.out.println(\"The detected path of length \"+dlen+\":\");
disppath(tpath, detlen);
if (detlen<=len_short){
len_short=detlen;
System.arraycopy(tpath, 0, spath, 0, detlen);
}
System.out.println(\" 0 \");
return;
}
searchnewpath(row-1, coln, tpath, detlen);
searchnewpath(row, coln-1, tpath, detlen);
searchnewpath(row, coln+1, tpath, detlen);
searchnewpath(row+1, coln, tpath, detlen);
}
public static void main(String[] args)
{
int rw,cl,k;
int dpath[];
int detlen;
DSPInMaze obj=new DSPInMaze();
detpath=new int[obj.num_of_rows*obj.num_of_cols];
for (k=0;k<obj.num_of_rows*obj.num_of_cols;k++){
obj.spath[x]=-1;
dpath[x]=-1;
}
obj.len_short=obj.num_of_rows*obj.num_of_cols+1;
dlen=0;
System.out.println(\"The Maze is displayed as shown:\");
for (rw=0;rw<obj.num_of_rows;rw++){
for (cl=0;cl<obj.num_of_cols;cl++){
if (r==b_of_row && c==b_of_col)
System.out.print(\"S\");
else if (r==e_of_row && c==e_of_col)
System.out.print(\"k\");
else if (obj.t_maze[rw][cl]==0)
System.out.print(\" .. \");
else System.out.print(\"|\");
}
System.out.println(\" * \");
}
System.out.println(\" 0 \");
System.out.println(\"Searching For the Paths!!!!!\");
obj.searchnewpath(b_of_row, b_of_col, detpath, detlen);
System.out.println(\" \");
obj.disppath(obj.spath, obj.len_short);
}
}


