1, the concept of recursion
- Calls itself
- Each call incoming variables are different
2, how recursive calls
3, recursion should abide by the rules
- The implementation of a method, it creates a new protected independent space (stack space)
- The method is independent of local variables do not affect each other, such as the variable n
- Recursive conditions must withdraw, otherwise it is infinite recursion, reported stackOverFloweError (stack overflow error)
- When a methodology is finished, or face returm, it will return, who keep calling, who will return the results to
- If it is a reference type variable (such as arrays) methodology used, it will share the type of reference data
4, maze problem solving
First with a two-dimensional array to simulate a table, the table assigned to part 1 move on to the wall (in red)
public static void main (String [] args) { int [] [] = Map new new int [10] [10 ]; // Set the wall for ( int I = 0; I <10; I ++ ) { Map [ 0] [I] =. 1 ; Map [I] [ 0] =. 1 ; Map [I] [ . 9] =. 1 ; Map [ . 9] [I] =. 1 ; } // obstacles map [3] [1] = 1 ; Map [ . 3] [2] =. 1 ; // Map [. 1] [2] =. 1; // Map [2] [2] =. 1; // print the form for ( int I = 0; I <10; I ++ ) { for ( int J = 0; J <10; J ++ ) { of System.out.print (Map [I] [J] + "" ); } System.out.println (); } // for pathfinding setWay (Map, 1,1 ); // Print form System.out.println ( "find its way to complete" ); for ( int I = 0; I < 10; I ++ ) { for ( int J = 0; J <10; J ++ ) { of System.out.print (Map [I] [J] + ""); } System.out.println(); } }
Wayfinding setting method, in accordance with the policy (below right upper left) to set up one by one recursively, if we can go through, this route is identified as 2, a dead end (not arrived at the designated end point) recursive backtracking, the road traveled home 3
Determine the feasibility of this road: 1 is the wall, walked the streets is 2, 3 is a dead end road
// set the policy process (lower left upper right) public static Boolean setWay ( int [] [] Map, int I, int J) { // Map map showing an array, i, j represent coordinates IF (Map [. 8] [. 8] == 2) { // begin before determining whether the node is found return to true ; } the else { iF (Map [I] [J] == 0 ) { Map [I] [J] 2 =; // through the place are first set to 2, the set of back. 3 IF (setWay (Map,. 1 + I, J)) { // the return to true ; } the else IF (setWay (Map, I, J +. 1)) {// the right return to true ; } the else IF (setWay (Map,. 1-I, J)) { // the return to true ; } the else IF (setWay (Map, I, J-. 1)) { // left return to true ; } the else { Map [I] [J] =. 3; // the lookup fails, the route and set back. 3 return to false ; } } the else { return to false ; } }
Operating results are:
Note: different options strategies, to find the path of the maze is different
How to get the shortest path?
Statistics different strategies can be generated route, to compare the shortest path through a strategy of acquisition path length policy
This strategy, as described above, the path length statistics
// Statistics path length int COUNT = 0; // length for ( int I = 0; I <10; I ++ ) { for ( int J = 0; J <10; J ++ ) { IF (Map [I] [J] == 2) { // when the path identification is available is 2 COUNT + =. 1 ; } } } System.out.println ( "pathlength" + count);