Python implements the tower-based recursive algorithm

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

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:

For more python-related articles on the implementation of Jakarta Tower recursive algorithms, please follow the PHP Chinese network!

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.