Recursive related Tower of Hanoi hanoi

      Speaking recently between unintentional recursion, I found almost forgotten, so the idea of ​​finishing the next, going to get hold of a note, or every time to re-think, very tired

Recursive problems, perhaps understanding the students may be able to explain in one sentence clearly, the students do not understand how to say there is no way to understand!


The basic concept of recursion:

Program that calls itself is called a recursive programming skills,
is a function that calls itself. In its definition of a function that calls itself a way to indirectly or directly,
it is usually a large, complex problem into a similar problem with the original scale minor problems to solve,

It can greatly reduce the amount of code that capacity recursive finite infinite set of statements to define the object.

In fact, call your own!

Internet is a vivid metaphor to say:
you open the front door and saw the house there is also a door.
You walked over and found the hands of the key can open it, you opened the door and found there is also a door, you open it to continue.
After several times, you open the front door, and found only one house, no door.

Then, you start to backtrack, each back to a house, you count them again, went to the entrance, you can use it to answer you in the end you put the key to open a few doors.


Recursive three elements:
1, a clear recursive termination condition; (no door)
2, gives a recursive approach when termination; (each into a door when the do something, or do nothing, to go back )

3, extracted repetitive logic, downsizing problem. (With a key to open the door to go)


Examples recursive: 1 + 2 + 3 ... + n =?

static void main public (String [] args) {
System.out.println (Addition (. 4));
}
/ **
? * 1 + ... + n-2. 3 = +
* if the addition order has been added from a n, to find the sum of time n is the number of
* @param I
* @return
* /
public static int Addition (I int) {
IF (I> 0) {
return Addition (I -. 1) + I; // (with a key open the door in)


when} else {// less than 1 (termination condition)
return 0; // returns zero (when the recursive termination approach)
}


}

The following is a flowchart of a routine


Tower of Hanoi realization
of Hanoi allusions:
Center of the World Benares (in northern India) holy temple,
a brass plate needle stuck three gems. Hindu God Lord Brahma at the time of creation of the world,
in which a needle from bottom to top to put on the descending of 64 gold piece, which is called the Tower of Hanoi.
Day or night, there is always a monk in these gold pieces move in accordance with the following rules: only one moving one, no matter on which needles, platelets must be large above.
The monks predicted that when all the pieces are put gold from Brahma that needle moved to another time on a pin,
the world will destroy in a clap of thunder, the Vatican and towers, temples and also beings will perish together


to solve the problem key, ignoring small details, large focus steps:
a the column "total n-1" is moved to the disks by means of disk B C drive, a large complete procedure
a column of the remaining "on n" disks moved directly to C plate, finishing a big process

"Total n-1" th disc by the disc A is moved to the B column C, and a large complete process


Implemented in code is:


public static void main(String[] args) {
String A="A";
String B="B";
String C="C";
int i = 3;
hanoi(i,A,B,C);
System.out.println(numCount);
}
/**
 * Hanoi
 */
public static void hanoi(int i,String A,String B,String C) {
numCount++;
if (i == 1) {
System.out.println("从"+A+"拿出  "+i+" 移动到"+C);
}else{
hanoi(i-1,A,C,B);
System.out.println("从"+A+"拿出  "+i+" 移动到"+C);
hanoi(i-1,B,A,C);
}

}



By C to A ( "common" n-1 th) to B
original column -> auxiliary column
C becomes the secondary, so that in the second place, becomes a target B;
Hanoi (. 1-I, A, C, B );


B by the A ( "common" n-1) th movement to C
plurality: auxiliary pillar -> target column
A is a secondary, so that in second position, the target variable C;
Hanoi (. 1-I, B, A, C);

The following is a recursive flowchart HANOR:





Published 13 original articles · won praise 1 · views 1500

Guess you like

Origin blog.csdn.net/gaoyang426/article/details/80678027
Recommended