Analysis of python recursive functions and Hanoi Tower problems, analysis of python recursive Hanoi

Source: Internet
Author: User

Analysis of python recursive functions and Hanoi Tower problems, analysis of python recursive Hanoi

Recursive functions:

The function calls its own function.

Take the n factorial as an example:

F (n) = n! = 1x2x3x4 x .. x (n-1) x (n) = n x (n-1 )!

def factorial(n):   if n==1:     return 1   return n * f(n-1)

// The call process is as follows:

>>f(5)>>5 * f(4)>>5 * 4 * f(3)>>5 * 4 * 3 * f(2)>>5 * 4 * 3 * 2 * f(1)>>5 * 4 * 3 * 2 * 1>>120

From the above example, we can intuitively see That recursive functions are constantly calling their own functions until n = 1 (function exit ).

About Hanoi:

Rules:

1. Three columns, A, B, C

2. The plates on pillar A are arranged from small to large. The top is the smallest and the bottom is the largest.

3. Move the plate on A to C. It is always maintained during the moving process. The biggest part is below and the smallest part is above.

Assume that there is A plate on column A, which can be directly moved from column A to column C:

A --> C

Assume that there are two plates on column A, and you need to use column B to move them to column C:

A --> B

A --> C

B --> C

Move the top disk (2-1) of A to B, move the remaining disk in A to C, and finally move the disk in B to C.

Assume that there are three plates on column A. Use B to move the two disks on column A, move the largest disk on column A to column C, and finally move the disk on column B to column C.

A --> C

A --> B

C --> B // move the first two plates of A to B.

A --> C // move the largest plate on A to C

B -->

B --> C

A --> C // move the plate on B to C in the next three steps

The principle is to move the (n-1) block Disk On A to B, then the remaining and largest block in A to C, and finally the (n-1) block disk on B to C.

def Hanoi(n , a, b, c):  if n==1:    print (" Hanoi Tower move", a, "-->", c)    return  Hanoi(n-1, a, c, b)  Hanoi(1, a, b, c)  Hanoi(n-1, b, a, c)print (" When there is 1 ring on A")Hanoi(1, 'A', 'B', 'C')print (" When there are 2 rings on A")Hanoi(2, 'A', 'B', 'C')print (" When there are 3 rings on A")Hanoi(3, 'A', 'B', 'C')print(" When there are 4 rings on A")Hanoi(4, 'A', 'B', 'C')

The above is a small series of python recursive functions and Hanoi Tower issues, I hope to help you, if you have any questions, please leave a message, the small series will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.