public class MazeArray {
public static void main(String[] args) throws FileNotFoundException{
File inputFile = new File("maze1.txt");
Scanner sc = new Scanner(inputFile);
int maze[][] = null;
maze = createMaze(sc, maze);
mazePathFinder(sc, maze);
printMaze(maze);
}
public static int[][] createMaze(Scanner sc, int[][] maze){
int arrayRowSize = sc.nextInt();
int arrayColumnSize = sc.nextInt();
maze = new int[arrayRowSize][arrayColumnSize];
for(int i = 0; i < arrayRowSize; i++){
for(int j = 0; j < arrayColumnSize; j++){
maze[i][j] = sc.nextInt();
}
}
return maze;
}
public static void printMaze(int[][] maze){
for(int i = 0; i < maze.length; i++){
for(int j = 0; j < maze[i].length; j++){
System.out.print(maze[i][j]);
}
System.out.println();
}
}
public static void mazePathFinder(Scanner sc, int[][] maze){
Stack mazeStack = new Stack();
int count = 0;
boolean done = false;
String rowCoordinates = sc.next();
String columnCoordinates = sc.next();
int row = Integer.parseInt(rowCoordinates);
int column = Integer.parseInt(columnCoordinates);
Coordinate startingCoord = new Coordinate(row, column);
mazeStack.push(startingCoord);
count++;
maze[row][column] = 1;
//Looks North, if the value is a 0, converts it to 1 and pushes coordinates onto the stack
try{
if(maze[row - 1][column] == 0){
row -= 1;
maze[row][column] = 1;
Coordinate secondCoord = new Coordinate(row, column);
mazeStack.push(secondCoord);
count++;
}
}
catch(ArrayIndexOutOfBoundsException aiooe){
}
//Looks East
try{
if(maze[row][column + 1] == 0){
column += 1;
maze[row][column] = 1;
Coordinate secondCoord = new Coordinate(row, column);
mazeStack.push(secondCoord);
count++;
}
}
catch(ArrayIndexOutOfBoundsException aiooe){
}
//Looks South
try{
if(maze[row + 1][column] == 0){
row += 1;
maze[row + 1][column] = 1;
Coordinate secondCoord = new Coordinate(row, column);
mazeStack.push(secondCoord);
count++;
}
}
catch(ArrayIndexOutOfBoundsException aiooe){
}
//Looks West
try{
if(maze[row][column - 1] == 0){
column -= 1;
maze[row][column] = 1;
Coordinate secondCoord = new Coordinate(row, column);
mazeStack.push(secondCoord);
count++;
}
}
catch(ArrayIndexOutOfBoundsException aiooe){
}
try{
while(!done){
Coordinate nextCoord = new Coordinate(row, column);
//Pushes nextCoord to stack if == 0
if (maze[row - 1][column] == 0){
maze[row - 1][column] = 1;
row -= 1;
mazeStack.push(nextCoord);
count++;
continue;
}
//Pushes nextCoord to stack if == 0
else if(maze[row][column + 1] == 0){
maze[row][column + 1] = 1;
column += 1;
mazeStack.push(nextCoord);
count++;
continue;
}
//Pushes nextCoord to stack if == 0
else if(maze[row + 1][column] == 0){
maze[row + 1][column] = 1;
row += 1;
mazeStack.push(nextCoord);
count++;
continue;
}
//Pushes nextCoord to stack if == 0
else if(maze[row][column - 1] == 0){
maze[row][column - 1] = 1;
column -= 1;
mazeStack.push(nextCoord);
count++;
continue;
}
mazeStack.pop();
row = nextCoord.getRow();
column = nextCoord.getColumn();
break;
}
}
catch(ArrayIndexOutOfBoundsException aiooe){
done = true;
}
Stack reorderCoords = new Stack();
for(int i = 0; i < count - 1; i++){
reorderCoords.push(mazeStack.pop());
}
for(int i = 0; i < count - 1; i++){
System.out.println(reorderCoords.pop());
}
}
}
ThisIsMeOrIsIt 0 Newbie Poster
NormR1 563 Posting Sage Team Colleague
ThisIsMeOrIsIt 0 Newbie Poster
NormR1 563 Posting Sage Team Colleague
ThisIsMeOrIsIt 0 Newbie Poster
NormR1 563 Posting Sage Team Colleague
ThisIsMeOrIsIt 0 Newbie Poster
NormR1 563 Posting Sage Team Colleague
ThisIsMeOrIsIt 0 Newbie Poster
NormR1 563 Posting Sage Team Colleague
ThisIsMeOrIsIt 0 Newbie Poster
ThisIsMeOrIsIt 0 Newbie Poster
ThisIsMeOrIsIt 0 Newbie Poster
ChaiNeeys commented: can you show the output? +0
ThisIsMeOrIsIt 0 Newbie Poster
ThisIsMeOrIsIt 0 Newbie Poster
NormR1 563 Posting Sage Team Colleague
ChaiNeeys 0 Newbie Poster
stultuske 1,116 Posting Maven Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.