Recursion
1. The concept of recursion
That calls itself recursively, passing different variables on each call, recursion can help programmers to solve complex problems, and can make the code becomes simple
2. The need to comply with important rules of recursion
1 ) a method when executed, create a new protected independent space (stack space) 2 local variable) of the process is independent, does not affect the mutual 3 ) If the reference type variable is used in the process (such as arrays) will share the data type of the reference 4 ) recursive approach must withdraw to the condition of recursion, or is infinite recursion, the emergence of a StackOverflowError 5 ) when a method is finished, or encountered return, will return. Who will abide by the results back to who to call, and when finished or return method, this method also finished
3. maze
Ideas:
1 the Map represents a map, i, j represents a departure from the map of the location (i, J) 2 If the ball comes to the Map [ 6 ] [ 5 ] position, indicating to find the exit 3 convention: map [i] [j] is 0 for this road has not gone through, 2 pass road is gone, and 3 represents the road traveled but did not go through 4 in the maze, you need to determine a strategy: next -> Right -> on - left>, if a dead end, on back
Specific code
com.ujiuye.recursion Package; public class MiGong { public static void main (String [] args) { // create a two-dimensional array, analog labyrinth int [] [] = Map new new int [ . 8 ] [ . 7 ]; / / represents a wall, the upper and lower set wall for ( int I = 0 ; I < . 7 ; I ++ ) { Map [ 0 ] [I] = 1 ; Map [ . 7 ] [I] = 1 ; } // left and right is also set to a wall for (int I = 0 ; I < . 7 ; I ++ ) { Map [I] [ 0 ] = . 1 ; Map [I] [ . 6 ] = . 1 ; } // set two baffles Map [ . 3 ] [ . 1 ] = . 1 ; Map [ . 3 ] [ 2 ] = . 1 ; . the System OUT .println ( " case maze " ); for ( int I = 0 ; I < . 8 ; I ++) { For ( int J = 0 ; J < . 7 ; J ++ ) { . The System OUT .print (Map [I] [J] + " " ); } . The System OUT .println (); } setWay (Map, . 1 , . 1 ); the System. OUT .println ( " balls through, and identifies where the maze " ); for ( int I = 0 ; I < . 8 ; I ++ ) { for (int J = 0 ; J < . 7 ; J ++ ) { the System. OUT .print (Map [I] [J] + " " ); } . the System OUT .println (); } } public static Boolean setWay ( int [] [ ] Map, int I, int J) { // If the map [6] [5] has gone and can go through, there is shown a labyrinth IF (Map [ . 6 ] [ . 5 ] == 2 ) { return to true ; } else{ // If there is no maze, can go way need lo @ 1 If 0 indicates not passed, it is necessary to try to go again IF (Map [I] [J] == 0 ) { // first passage has gone through the set, or 2 through 3 represent, if it can go through // If a dead end, and finally changes the value of this line 3 Map [I] [J] = 2 ; // according to the upper right down-left order // at 1, i + 1, a recursive call to see if it can go through the line iF (setWay (Map, + I . 1 , J)) { return to true ; // 2 the right, j + 1, recursive calls see whether this line to go through } the else iF (setWay (Map, I, J + . 1)) { Return to true ; // the 3, i-1, a recursive call to see if this line can go through } the else IF (setWay (Map, I- . 1 , j)) { return to true ; // . 4 left, j -1, a recursive call to see if this line can go through } the else iF (setWay (Map, I, J- . 1 )) { return to true ; } the else { // the left did not go through the upper right map [i] [j ] = . 3 ; return to false; } // represents the road traveled, 1 or 2 or 3, no longer follow the road traveled } the else { return to false ; } } } }
4. The eight queens problem
Thinking;
1 ) The first queen to discharge a first row and first column 2 ) the second column of the second row queens on a first, and then determines whether or not ok, the OK if not, continue in the second column, third column. . . Turn all the columns are done and find a suitable location 3 ). . . 4 ) When the time to get a correct solution, when the stack falls back on a stack, it will start back, all right solution coming into the first column of the first Queen of all get 5 ) and then continue it first put the Queen in the second column, the cycle continues
Specific code
com.ujiuye.recursion Package; public class Queue8 { // definition has several Queen int max = . 8 ; static int COUNT = 0 ; // define a one-dimensional array, the number of rows is not displayed, the number of columns of the display array // arr [i] i represents the i + 1-queens, arr [i] represents the arr [i] +1 column int [] Array = new new int [max]; public static void main (String [] args) { Queue8 queue8 = new new Queue8 (); // from the first start queen placed queue8.check ( 0 ); the System. OUT.printf ( " total seed placement% d " , COUNT); } // write method of placing a n-queens Private void Check ( int n) { // n maximum of 7, 8 is equal to if all instructions placed complete iF (n-== max) { Print (); return ; } // turn into Queen, determines whether a collision, i denotes a first column i + 1 for ( int I = 0 ; I <max; I ++ ) { // n + 1-queens in the first column Array [n-] = I; // Analyzing the n + 1 placing queens to the i + 1 row, whether a collision iF (Judge (n-)) { //If not conflict, to lay down a queen Check (n + 1 ); } // number of columns, if a conflict, put the queen discharge position plus 1, i.e. for loop } } // See when placed n-queens, determines whether or not the position conflict // each queen line, the position is determined there is no straight line or column oblique Private Boolean Judge ( int n) { // Add fourth Queen, n = 3, three comparisons necessary to front, n is the number of times for ( int I = 0 ; I <n-; I ++ ) { // before || indicates a column is a vertical // the equal spacing represents || include, a slash IF (Array [I] == Array [n-] || the Math.abs (Ni) == the Math.abs (Array [n-] - Array [I])) { return to false; } } return true; } //输出皇后摆放的位置array={0,4,7,5,2,6,1,3} //h1:1,h2:5,h3:8,h4:6,h5:3,h6:7,h7:2,h8:4 private void print() { count++; for(int i = 0;i < array.length;i++) { System.out.print(array[i]+""); } System.out.println(); } }
。