java - Maze Traversal Algorithm Using Recursion -
i'm trying traverse maze using recursion class. provided template , need input the process of traversing maze; not allowed alter code in way besides being plugged in after this:
public boolean mazetraversal (char maze2[][], int x, int y)
i have been having hard time , don't know missing. still , ultra noob when comes java , programming in general. hints appreciated.
// exercise 18.20 solution: maze.java //program traverses maze. import java.util.scanner; package maze; public class maze { public static void main( string args[] ) { } /** * @param args command line arguments */ static final int down = 0; static final int right = 1; static final int = 2; static final int left = 3; static final int x_start = 2; static final int y_start = 0; static final int move = 0; static char maze [][] = { { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' }, { '#', '.', '.', '.', '#', '.', '.', '.', '.', '.', '.', '#' }, { 's', '.', '#', '.', '#', '.', '#', '#', '#', '#', '.', '#' }, { '#', '#', '#', '.', '#', '.', '.', '.', '.', '#', '.', '#' }, { '#', '.', '.', '.', '.', '#', '#', '#', '.', '#', '.', 'f' }, { '#', '#', '#', '#', '.', '#', '.', '#', '.', '#', '.', '#' }, { '#', '.', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#' }, { '#', '#', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#' }, { '#', '.', '.', '.', '.', '.', '.', '.', '.', '#', '.', '#' }, { '#', '#', '#', '#', '#', '#', '.', '#', '#', '#', '.', '#' }, { '#', '.', '.', '.', '.', '.', '.', '#', '.', '.', '.', '#' }, { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' } }; static scanner scanner = new scanner (system.in); //method calls mazetraversal correct starting point , direction public void traverse () { boolean result = mazetraversal (maze, x_start, y_start); if (!result) system.out.println ("maze has no solution."); } //end method traverse //traverse maze recursively public boolean mazetraversal (char maze2[][], int x, int y) { // completed //check boundaries if (x < 0 || y < 0 || x >= maze.length || y >= maze[0].length) return false; //base case - did reach finish? if(maze [x][y] == 'f') return true; //check if valid point if (maze [x][y] != '.' || maze[x][y] != 's') // hit wall, not valid return false; //must on path, may right path //breadcrumb maze [x][y] = '.'; //up if (mazetraversal (x+1,y)) { maze [x][y]= '#'; return true; } if (mazetraversal (x,y+1)) { maze [x][y] = '#'; return true; } // can't go further return false; } } //draw maze public void printmaze() { // each space in maze ( int row = 0; row < maze.length; row++) { (int column = 0; column < maze [row].length; column++) { if (maze [x][y] == '0') system.out.print ("."); else system.out.print (" " + maze [x][y]); } system.out.println(); } system.out.println(); } }
on given point, have 4 directions move if or not on edge, given constraint cannot move diagonally or through walls. start empty 2x2 matrix, , on every step can take near starting point, mark "1" because takes 1 step.
afterwards, mark "2" adjacent legal steps not have number.
next, mark "3" adjacent legal steps next 2s not have number.
ultimately, end filling whole matrix legal moves , minimum amount of moves lead spot.
by marking number spot have not been assigned, avoid backtracking or stepping on step can has been reached earlier. ensures matrix filled "best possible routes."
(see recursion comes in? :) )
Comments
Post a Comment