Skip to content

Instantly share code, notes, and snippets.

@ryandhubbard
Created August 16, 2016 22:01
Show Gist options
  • Save ryandhubbard/19714c1602f74822e7dcc8f154aba373 to your computer and use it in GitHub Desktop.
Save ryandhubbard/19714c1602f74822e7dcc8f154aba373 to your computer and use it in GitHub Desktop.
Recursive Maze Solver
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
2 0 1 1 1 0 1 1 1 0 1 0 1 1 1 1 1 0 1 0 1 1 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
3 0 0 0 1 0 0 0 1 0 1 0 0 0 1 0 1 0 1 0 1 0 0 0 1 0 1 0 1 0 1 0 0 0 0 0 0 0 1 0 0 0
4 0 1 0 1 1 1 1 1 1 1 0 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 0 1 0 1 1 1 0 1 0 1 0 1 0 1 0
5 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 1 0 0 0 1 0 1 0 0 0 1 0
6 0 1 1 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 0 1 1 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1 0 1 1 1 0
7 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 1 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0 1 0 0 0
8 0 1 1 1 1 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 1 1 1 1 1 1 0 1 0 1 1 1 1 1 1 1 0
9 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0 1 0 1 0
10 0 1 1 1 0 1 1 1 1 1 1 1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 1 1 0 1 1 1 1 1 0 1 1 1 0
35 0 1 0 0 0 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 1 0 1 0 1 0 1 0 1 0 0 0 0 0
36 0 1 1 1 0 1 0 1 0 1 0 1 0 1 1 1 1 1 0 1 0 1 1 1 0 1 1 1 0 1 0 1 0 1 0 1 1 1 1 1 0
37 0 1 0 0 0 0 0 1 0 1 0 0 0 1 0 1 0 1 0 0 0 1 0 1 0 0 0 1 0 1 0 1 0 0 0 0 0 1 0 1 0
38 0 1 0 1 1 1 1 1 0 1 0 1 0 1 0 1 0 1 0 1 1 1 0 1 0 1 1 1 0 1 0 1 1 1 1 1 0 1 0 1 0
39 0 1 0 0 0 1 0 1 0 1 0 1 0 1 0 0 0 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 1 0
40 0 1 1 1 0 1 0 1 1 1 1 1 1 1 1 1 0 1 0 1 1 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0
41 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class MazeSolver {
public static void main(String[] args) throws IOException {
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
try {
br = new BufferedReader(new FileReader("Maze - Input.csv"));
} catch (FileNotFoundException ex) { //Cannot Find File
System.out.println("\nTrouble Opening File\n");
System.exit(0); //Terminates the program if cannot find file
}
try {
line = br.readLine();
System.out.println("\nMaze - Input.csv is being Read\n");
} catch (IOException ex) {
}
String[] values = line.split(cvsSplitBy);
int [][]maze = new int[values.length-1][values.length-1];
for(int i = 0;i<values.length-1;i++)
{
line = br.readLine();
String[] values2 = line.split(cvsSplitBy);
for(int j = 0;j<values2.length-1;j++)
maze[i][j] = Integer.parseInt(values2[j+1]);
}
MazeSolver mazeSolver1 = new MazeSolver();
mazeSolver1.initialize(maze);
System.out.println(" Maze Before the Solution\n");
mazeSolver1.printMaze();
mazeSolver1.mazeSolve();
System.out.println("\n Maze After the Solution\n'P' is the path to follow to exit the Maze\n"
+ "'V' Cells were visited but not part of the final path\n");
mazeSolver1.printMaze();
}
public MazeSolver() {
}
public int [][] mazeArray;
public void initialize(int[][] maze)
{
mazeArray = new int[maze.length][maze[0].length];
wasHere = new boolean[maze.length][maze[0].length];
correctPath = new boolean[maze.length][maze[0].length];
for(int i = 0;i<maze.length;i++)
{
for(int j = 0;j<maze[0].length;j++){
mazeArray[i][j] = maze[i][j];
wasHere[i][j] = false;
correctPath[i][j] = false;
}
}
}
public void mazeSolve()
{
int []start = {0,0};
int []end = {mazeArray.length-1,0};
for(int i= 0;i<mazeArray[0].length;i++)
if(mazeArray[0][i] == 1)
start[1] = i;
for(int i= 0;i<mazeArray[0].length;i++)
if(mazeArray[mazeArray.length-1][i] == 1)
end[1] = i;
startX = start[1];
startY = start[0];
endX = end[1];
endY = end[0];
boolean b = recursiveSolve(startX, startY);
for(int i = 0;i<mazeArray.length;i++)
{
for(int j = 0;j<mazeArray[0].length;j++){
if(wasHere[i][j] == true)
mazeArray[i][j] = 3;
if(correctPath[i][j] == true)
mazeArray[i][j] = 2;
}
}
}
int[][] maze; // The maze
boolean[][] wasHere;
boolean[][] correctPath; // The solution to the maze
int startX, startY; // Starting X and Y values of maze
int endX, endY; // Ending X and Y values of maze
public boolean recursiveSolve(int x, int y) {
if (x == endX+1 && y == endY) return true; // If you reached the end
if (mazeArray[y][x] == 0 || wasHere[y][x]) return false;
// If you are on a wall or already were here
wasHere[y][x] = true;
if (x != 0) // Checks if not on left edge
if (recursiveSolve(x-1, y)) { // Recalls method one to the left
correctPath[y][x] = true; // Sets that path value to true;
return true;
}
if (x != mazeArray[0].length - 1) // Checks if not on right edge
if (recursiveSolve(x+1, y)) { // Recalls method one to the right
correctPath[y][x] = true;
return true;
}
if (y != 0) // Checks if not on top edge
if (recursiveSolve(x, y-1)) { // Recalls method one up
correctPath[y][x] = true;
return true;
}
if (y != mazeArray.length- 1) // Checks if not on bottom edge
if (recursiveSolve(x, y+1)) { // Recalls method one down
correctPath[y][x] = true;
return true;
}
return false;
}
public void printMaze()
{
for(int i = 0;i<mazeArray.length; i++)
{
for(int j = 0;j<mazeArray[0].length;j++){
if(mazeArray[i][j]<2)
System.out.print(mazeArray[i][j]);
else if(mazeArray[i][j] == 2)
System.out.print('P');
else if(mazeArray[i][j] == 3)
System.out.print('V');
}
System.out.println();
}
}
}
Maze - Input.csv is being Read
Maze Before the Solution
00000000000000000000000000010000000000000
01110111010111110101110101111111111111110
00010001010001010101000101010100000001000
01011111110111011111111111010111010101010
01000000010100000000000101010100010100010
01111111011111011101110101010111111101110
00000100000000010101010100000001010001000
01111111011101110111011111111101011111110
00000100000100000001000100000001010001010
01110111111111111101011111111101011101010
00010000010000000100010000010000010001000
01111111110111111111111111011101110111110
00000101000100000101010101010001000001000
01011101011101111101010101010111011101110
01010001000100010001000101000001000100010
01111101010111011101011101111101111111010
00000001010100000000010001000000010001000
01111111011111110111111101111111011101010
00010000000101010001000100010001000101010
01110101010101010111011101011101010101110
01000101010101000001000001000100010000000
01011101011101111101011111111111111101110
00010101010001010000000001000101010001000
01010111110111011101110111011101011111110
01000001010001010101010000000101000101010
01110111011101010111010111011101011101010
00010001010001010001000001000101000100010
01011101011101010111011111111101110111010
01000101010000010001010000010101000101000
01111111011101111101011101110101110101110
00010001000100000100000101010001000101000
01110101010111110101111101010111011101110
01000101010100010000010001000101000001000
01110111110101110111110111011101111101110
01000100000101000001000100010101010100000
01110101010101111101011101110101010111110
01000001010001010100010100010101000001010
01011111010101010101110101110101111101010
01000101010101000100000101000001000100010
01110101111111110101111101110111011101110
00000000000000000100000000000000000000000
Maze After the Solution
'P' is the path to follow to exit the Maze
'V' Cells were visited but not part of the final path
000000000000000000000000000P0000000000000
0VVV0VVV0V0VVVVV0V0VVV0V0PPP1111111111110
000V000V0V000V0V0V0V000V0P010100000001000
010VVVVVVV0VVV0VVVVVVVVPPP010111010101010
010000000V0V00000000000P01010100010100010
011111110VVVVV0VVV0VVV0P01010111111101110
000001000000000V0V0V0V0P00000001010001000
0111111101110VVV0VVV0VVPVVVVVV01011111110
0000010000010000000V000P00000001010001010
0111011111111111110V0PPP11111101011101010
000100000100000001000P0000010000010001000
01111111110PPPPPPPPPPP1111011101110111110
00000101000P00000101010101010001000001000
010111010VVP01111101010101010111011101110
01010001000P00010001000101000001000100010
011111010V0PVV011101011101111101111111010
000000010V0P00000000010001000000010001000
011111110VVPVVVV0111111101111111011101010
00010000000P0V0V0001000100010001000101010
01110V0V0V0P0V0V0111011101011101010101110
01000V0V0V0P0V000001000001000100010000000
010VVV0V0PPP0VVVVV01011111111111111101110
000V0V0V0P000V0V0000000001000101010001000
0V0V0VVVVP0VVV0VVV0VVV0111011101011111110
0V00000V0P000V0V0V0V0V0000000101000101010
0VVV0VVV0PVV0V0V0VVV0V0111011101011101010
000V000V0P000V0V000V000001000101000100010
0V0VVV0V0PVV0V0V0VVV011111111101110111010
0V000V0V0P00000V000V010000010101000101000
0VVVVVVV0PPP0VVVVV0V011101110101110101110
000V000V000P00000V00000101010001000101000
0VVV0V0V0V0PPPPP0V01111101010111011101110
0V000V0V0V01000P0000010001000101000001000
0VVV0VVVVV010PPP0111110111011101111101110
0V000V0000010P000001000100010101010100000
0VVV0V0101010PPPPP01011101110101010111110
0V000001010001010P00010100010101000001010
0V011111010101010P01110101110101111101010
0V000101010101000P00000101000001000100010
0VVV0101111111110P01111101110111011101110
00000000000000000P00000000000000000000000
Test Case (1) for File missing
————————————————————————————————
Trouble Opening File
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment