Python implements Hanoi recursive algorithm

Source: Internet
Author: User
Learn to recursion when there is a Hanoi practice, Hanoi should be learning computer recursive algorithm of the classic introductory case, so I think you can write a blog to express their views. This markdown editor is not very likely to use, may be written a bit ugly format, you crossing a lot of forgive me.

On the internet to find a Zhang Hanota picture, Hanoi is the use of the middle pillar to the leftmost column on the top of the disk from large to small stack up, plainly speaking is C to be the same as the original


Talk less, light the code first

def move (n, a, buffer, c):  if (n = = 1):    print (A, "-", c)    return Move (  n-1, A, c, buffer)  move (1, a, b Uffer, c)  move (n-1, buffer, A, c) move (3, "A", "B", "C")

The first is to define a moving function, four parameters represent, the number of plates on a column, buffer is the B-column, named buffer for easy to understand, as the name implies is a move to C buffer. and C is the target pillar.
Let's read the function code.
Recursive general wording, there must be an abort recursive loop condition, so in judging a column on the number of plates is 1 can abort recursive merge return, a column above only one time must be to move a to C, the focus is the following code, recursion is actually a very abstract algorithm, We have to use abstract thinking to think about Hanoi, and think of the plate on the A pillar as two, which is the plate above and the bottom plate, as shown


We don't care how many plates there are on it, and every time we do that, we move the bottom plate through buffer B to the C column.
Children's shoes are sure to think why to Jiangzi Mobile, in fact, this is a summary of it, you play Hanoi game you will find the law, in fact, this game is to keep all of the above all the way to the B, and then the last largest one to get C, and then the brains of the B moved to C, At this time you will find that the original B on the first through the empty is also a to store the current B above the n-1, and then the largest of the last move of B to C, where the law is manifested, can also be abstracted out of the method of movement, and can be used to design a program algorithm.

Let's use the abstract thought to interpret the remaining code

Move (N-1, A, c, buffer)

This code is to represent the n-1 of the column A above, which is moved to buffer buffers by C according to the rule from small to large. This function enters recursion.

Move (1, a, buffer, c)

When the above statement is completed, that is, after the recursive movement of the n-1 Plate is completed, the execution of this statement is to move a tray on the a column to C, the so-called bottom plate

Move (n-1, buffer, A, c)

The last step, just put a above the n-1 all moved to the buffer above, it is sure to move through a to C to complete the Hanoi movement Ah, so the last step is naturally just the n-1 through a when the buffer moved to the C column.
I'm going to write down the entire mobile process, with 3 on the A column as an example.

/** I have 3 plates of the Hanoi all through the code demonstration, according to the indentation principle, each indentation is a recursive function, each print will abort the current recursion, that is, each printing description: 1.N = 3, n = 2, n = 1 is the result of each execution if (n = = 1), I do not write judgment, believe that the child Shoes can also be understood, that is, n unequal and 1 o'clock minus 1 into recursion 2. Note that each time the A,b,c column enters the order of the function, do not be taken by the formal parameter with the wrong path, see the arguments of each function parameter **/move (3, "A", "B", "C") N=3://    Start moving from a n-1 that is 2 plates moving through C to B, to vacate the C for a last plate move moves (2, "a", "C", "B") n=2://Start with a recursive n=2, move the n-1 plate on the current a (' a ') bar through C (' B ') to B (' C ')    Move (1, "A", "B", "C") N=1://n=2 The first recursive completion, prints the result, executes the current sub-function remaining code print ("A", "-", "C") Move (1, "A", "C", "B") N=1:print ("A", "-", "B") Move (1, "C", "A", "B") N=1:print ("C", "--", "B")//To complete the n-1 above the column A That is, the movement of 2 plates//start moving the last plate on the A pillar to the C-pillar move (1, "A", "B", "C") N=1:print ("A", "-", "C")//go here to finish moving the last plate on the A-pillar to the C-pillar move (2, "B", "a "," C ") n=2://begins the second recursion of n=2, which is to move the current B (' B ') plate (n-1) through a (' a ') to C (' C ') on Move (1," B "," C "," a ") N=1: The second recursive completion of//n=2, prints the result and executes the current child The remaining code of the function print ("B", "-", "a") Move (1, "B", "A", "C") N=1:print ("B", "-", "C") Move (1, "A", "B", "C") n=1 : Print ("A", "-", "C")//go here to pass the plate on B through aMove to c,//the entire code executes and Hanoi moves to completion 

The final print result is:


Children's shoes understand the principle of Hanoi recursive algorithm, you can write a program to try, here just learn python recursion so with python, children's shoes can be implemented in other languages, Hanoi does help to understand the principle of recursion, recursion in the design of the importance of self-evident!


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.