Brief
The algorithm excerpt from Aha Lei book "Aha! Algorithm "Chapter IV, Section II title --DFS algorithm rescue Patricia. The text code written in C, bloggers by reading and understanding, re-implemented again by the Java code, in order to deepen the impression of DFS algorithm.
Game Settings
Maze cell composition n rows and m columns (n and m is less than or equal to 50), each cell is either a space or an obstacle, the obstacle is impassable, the requirements began to find a path to the starting point from the maze a maze the shortest path points.
Solution Ideas
First, using a two-dimensional array to store the maze, starting from the beginning assumed inlet labyrinth (0, 0), the target position (q, p), the purpose of this line is found from (0, 0) to (q, p) of the shortest path. If the entry point, the next step can only be to the right or go down, if it is to a certain point in the middle of the maze, you might go up both horizontally and vertically, only one try, we are simply here sequential definition, clockwise (i.e., right, down, left, top) direction and try them .
Another problem is worth noting, is already passing point, certainly do not need to try to calculate the next point , here for the convenience of judgment, can be marked using familiar barrel , it has passed the point marked in the bucket, after use the bit and then come back to clean barrel mark on the line. For obstacle, an obstacle to change direction to start again, this is to return to the next recursive loop on the line.
Note that when we traverse found the target point, but it may not be the shortest path is used, so after finding the target point, need to go back again to try to find the other direction until all possibilities have tried over, last minute piece of the shortest path to the output.
Now we use depth-first search algorithm DFS to try to implement this algorithm. Start dfs function begins, the current step is to think of what the next step and the current step is the same, what kind of boundary conditions.
We define the function to dfs three parameters x, y and STEP, respectively, to reach the next coordinates and the current number of steps is determined if the target position has been found, i.e. judges (x, y) whether the target position is equal to, simultaneously with taking the number of the current step to judge whether the minimum number of steps, a record is updated to the minimum step value.
In the specific code implementation, we should also focus on how to start the search from the four directions clockwise goals, how to navigate to the next point, and to determine whether cross-border, or whether it is an obstacle, or prior to this point has been problem of passing the like.
Code
. 1 public class MazeDfs { 2 . 3 / ** size maze matrix: n rows and m columns * / . 4 Private static Final Integer n-= 10, m = 10 ; . 5 / ** target position in the maze * / . 6 Private static Final Integer . 6 = Q, P =. 6 ; . 7 / ** minimum number of steps from the start to the target position line * / . 8 Private static Integer minStep = 999 ; . 9 / ** labyrinth map * / 10 Private static int [] [] maze = new new int[50] [50 ]; 11 / ** barrel, walking through the recording path location * / 12 is Private static int [] [] = Book new new int [50] [50 ]; 13 is 14 / ** 15 * recursive function 16 * go one step further, to see whether the current position of the target position, or continue down a forward direction 17 * @param the X- 18 * @param the y- 19 * @param the sTEP 20 * / 21 public void the DFS ( int the X-, int the y-, int STEP) { 22 is // definition of four directions, in accordance with the clockwise turn to the right, down, left, up 23 is int [] [] = {{0 Next,. 1}, {. 1, 0}, {-1, 0} , {0, -1 }}; 24 25 System.out.println ( "current coordinate point:" + X + "" + Y); 26 is 27 // determines whether the target position 28 iF (X == Y Q && == P) { 29 // updated minimum value of step 30 IF (sTEP < minStep) { 31 is minStep = sTEP; 32 } 33 is // accordance with the current mode is found, returns, continue to look for other ways 34 is return ; 35 } 36 37 [ //Four directions to find the enumeration process 38 is int TX, TY, K; 39 for (K = 0; K <. 4; K ++ ) { 40 // position into a next calculated point 41 is TX Next = X + [ K] [0 ]; 42 is TY Next = Y + [K] [. 1 ]; 43 is 44 is // judge the direction out of bounds next 45 iF (TX <0 || TX> TY = n-|| <|| 0 TY> = m) { 46 is Continue ; 47 } 48 49 // determine whether the point is an obstacle or have already passed 50 iF (Maze [TX] [TY] == 0 && Book [TX] [TY] == 0 ) { 51 // mark this point has been passed 52 is Book [TX] [TY] =. 1 ; 53 is // and then try the next point 54 is DFS (TX, TY, STEP +. 1 ); 55 // try end, unmark the point 56 is Book [TX] [TY] 0 = ; 57 is } 58 } 59 return ; 60 } 61 is 62 is public static void main (String [] args) { 63 is MazeDfs mazeDfs = new new MazeDfs (); 64 // initialization, the labyrinth matrix represented by two-dimensional array, space "0" means that the obstacle by "1" indicates that 65 // TODO 66 67 // from the search starting point, the starting position is assumed here that [0, 0], the start step number is 0 68 int the startx = 0, startY = 0, STEP = 0 ; 69 // tag in the tub has been subjected to the starting point 70 Book [the startx] [startY] =. 1 ; 71 is // starting from a first position 72 mazeDfs.dfs (the startx, startY, sTEP); 73 is 74 // output the minimum number of steps 75 System.out.println (String.format ( "from the start at least after step% d can reach the target location." , minStep)); 76 } 77 78 }
Reference material
1, "Aha! Algorithm "/ Ah Lei, the People's Posts and Telecommunications Press