Taking a maze involves a lot of experimentation and errors: go along a path, when you can't backtrack back, and try another option that you haven't tried before. However, a recursive algorithm is used.
masesearch.java//******************************** public class Masesearch {public static void main (string[] args) {Mase labyrinth = new Mase (); System.out.println (Labyrinth); if (Labyrinth.traverse (0,0)) System.out.println ("The Mase was successfully traversed!"); ElseSystem.out.println ("There is no possble path"); System.out.println (Labyrinth);}} mase.java//************************************** public class Mase {private final int tried = 3;private final int PATH = 7;private int[][] grid={{1,1,1 , 0,1,1,0,0,0,1,1,1,1}, {1,0,1,1,1,0,1,1,1,1,0,0,1}, {0,0,0,0,1,0,1,0,1,0,1,0,0}, {1 , 1,1,0,1,1,1,0,1,0,1,1,1}, {1,0,1,0,0,0,0,1,1,1,0,0,1}, {1,0,1,1,1,1,1,1,0,1,1,1,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0}, {1,1,1,1,1,1,1,1,1,1,1,1,1}};p ublic Boolean TraveRSE (int row,int column) {Boolean done = False;if (valid (Row,column))///Determine if 1, 1 true {Grid[row][column] = tried;if (row = g Rid.length-1 && column = = grid[0].length-1) done = True;else{done = Traverse (Row+1,column), if (!done) done = Traverse (row,column+1); if (!done) done = Traverse (Row-1,column), if (!done) done = Traverse (row,column-1);} if (done) Grid[row][column]=path;} return done;} Private Boolean valid (int row,int column) {boolean result = False;if (row>=0 && row<grid.length && Co Lumn >= 0 && Column < grid[row].length) if (grid[row][column] = = 1) result = True;return result;} Public String toString () {string result = "\ n"; for (int row = 0;row < grid.length;row++) {for (int column=0; Column<grid [row].length;column++) result + = Grid[row][column] + ""; result + = "\ n";} return result;}}
Java---Maze