Typical Case of implementing the tower of death recursive algorithm using python

Source: Internet
Author: User
In this article, we will share with you a classic example of python's implementation of Jakarta recursive algorithm. If you are interested, you can refer to the example of a yoga tower exercise when learning recursion, juita should be a classic entry-level case for learning computer recursive algorithms, so I think I can write an article to express my opinions. This markdown editor is not very easy to use yet. It may be a bit ugly to write it in a format. You may have to forgive me.
I found a picture of the tower on the Internet. The middle column is used to fold the disc on the leftmost column from large to small, to put it bluntly, c must be the same as.


To put it bluntly, please first highlight the code

def move(n, a, buffer, c):  if(n == 1):    print(a,"->",c)    return  move(n-1, a, c, buffer)  move(1, a, buffer, c)  move(n-1, buffer, a, c)move(3, "a", "b", "c")

The first step is to define a moving function. The four parameters represent the number of plates on the column, and the buffer is also the B column. The name is buffer, which is easy to understand. As its name suggests, it is a buffer for a to move to c. then c is the target pillar.
Let's read the function code.
There must be a condition for stopping recursive loops in the general method of recursion. Therefore, when determining the number of plates on column A as 1, the recursion can be aborted and returned, when there is only one column on the column, it must move a to c. The focus is on the following code. recursion is actually a very abstract algorithm, we need to use abstract thinking to think about the tower of Hanoi. Think of the plate on the column A as two parts, that is, the plate above and the bottom plate. If


We don't care about the number of plates above. Each operation is to move the bottom plate to the c column through the buffer B column buffer.
The kids shoes are definitely thinking about why they want to move the app with soy sauce. In fact, this is a summary. You will find the rule when you play the tower game by yourself, in fact, this game is constantly moving all the above methods to B, and then getting the largest one in a to c, and then racking your brains to move the one in B to c, at this time, you will find that in the original B, we should first store n-1 numbers on the current B through null, that is, a, and then move the largest and last values of B to c, the rule here is embodied. You can also abstract the moving method and design the program algorithm.

The following uses the abstract thinking to interpret the remaining code:

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

This code refers to moving n-1 column above the-pillar to the buffer through c according to the rules from small to large. This function is recursive.

Move (1, a, buffer, c)

When the preceding statement is executed, that is, after the recursive movement of N-1 plates is completed, execute this statement to move a plate on column A to column c, that is, the so-called bottommost plate.

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

The last step is to move the n-1 numbers above a to the buffer just now. You must move a to c to complete the movement of the entire tower, so the last step is to move the n-1 passed through a as the buffer to the column c.
Let me write down the entire movement process. Here are three examples on the column.

/** I demonstrated all the three pie towers by Code. According to the indentation principle, each indent is a recursive function, and each print ends the current recursion, that is, for each print Description: 1.n = 3, n = 2, n = 1 is the result of every execution of if (n = 1). No judgment is written here, I believe that the children's shoes can also understand, that is, when n is not equal to 1, 1 is subtracted to go to recursion 2. note that column a, column B, and column c enter the Function Sequence each time. Do not take the wrong path of the form parameter. Check that the real parameters of each function parameter **/move (3, "", "B", "c") n = 3: // start to move n-1 from a, that is, two plates are moved to B through c, to free up c for a to move the last plate move (2, "a", "c", "B") n = 2: // start a recursion of n = 2, move n-1 plates on the column a ('A') to B ('C') through c ('B') move (1, "a", "B ", "c") n = 1: // The first recursion of n = 2 is completed. print the result and run the print ("a", "-> ", "c") move (1, "a", "c", "B") n = 1: print ("a", "->", "B ") move (1, "c", "a", "B") n = 1: print ("c", "->", "B ") // here the n-1 above column a is the movement of two plates // start to move the last plate on column a to column c (1, "a", "B ", "c") n = 1: print ("a", "->", "c ") // move the last plate on the column to the c column here (2, "B", "a", "c") n = 2: // start the second recursion of n = 2, that is, the current B ('B') plate (n-1) through a ('A ') move to c ('C') and move (1, "B", "c", "a") n = 1: // n = 2. The second recursion is completed, print the result and execute the remaining code print ("B", "->", "a") move (1, "B", "", "c") n = 1: print ("B", "->", "c") move (1, "a", "B", "c ") n = 1: print ("a", "->", "c") // here, move the plate on B to c through a. // The entire code is executed, operation completed

The final result is:


After understanding the recursive algorithm principle of shuita, students can write a program to try it. Here we only learn Python recursion, so we use Python. Shoes can be implemented in other languages, tower of Hanoi can help understand the principle of recursion. The importance of recursion in programming is self-evident!

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.