rendered paste body//Name -
//Date -
//Lab -
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import static java.lang.System.*;
class Maze
{
private int[][] maze;
private boolean exitFound;
public Maze()
{
exitFound = false;
maze = new int[0][0];
}
public Maze(int[][] m)
{
exitFound=false;
maze = new int[m.length][m[0].length];
for(int r= 0; r<maze.length; r++)
{
for(int c =0; c<maze[r].length; c++)
{
maze[r][c]=m[r][c];
}
}
}
public void checkForExitPath(int r, int c)
{
if(r >= 0 && r < maze.length && c >= 0 && c < maze.length)
if(maze[r][c]==1)
{
maze[r][c] = 2;
if(isOutOfBounds(r,c))
exitFound=true;
else
{
checkForExitPath(r-1, c);
checkForExitPath(r+1, c);
checkForExitPath(r, c-1);
checkForExitPath(r, c+1);
}
}
}
public boolean isOutOfBounds(int r, int c)
{
if((r+1>=maze.length) || (c+1>=maze.length))
return true;
return false;
}
public String toString()
{
String output="";
return output;
}
}
public class Lab09g
{
public static void main( String args[] ) throws IOException
{
Scanner file = new Scanner(new File("lab09g.dat"));
while(file.hasNextLine())
{
int rc = file.nextInt();
int[][] maze2 = new int[rc][rc];
file.nextLine();
Maze test = new Maze(maze2);
test.checkForExitPath(0,0);
out.println(test);
}
}
}