Using Python3 recursion method to solve the problem of __python Tower

Source: Internet
Author: User

Hanoi Tower Problem:


from left to right a B C column large plate, small plate on, with the help of B column to move all plates from column A to column C, only one principle: the large plate can only be under the small plate.

If there are 3 plates, large small number, the smaller the more on the top, from the above to the plate in order numbered 1 (small), 2 (Medium), 3 (large), the principle of the following resolution reference to the number here.

As a child played this game, basically play to the 7th, the 8th is very impatient to play, and the operation of the movements are almost the same boring. Later learning programming, recognizing recursion, using recursion to solve Hanoi algorithm is the first algorithm that I learned in addition to the simple sorting algorithm.

as far as recursion is concerned, the simple way is to call itself within the method itself, there must also be an end point. If you understand the method call stack, it is very easy to understand the method of the call process, that is, from the main thread began to invoke the method of continuous stack and stack operation. The method is pushed into the stack, The end of the method is the process of the method stack, which guarantees the sequential flow of the method call. If the trace recursive invocation is found to be the same, it must eventually be the method that pops from the stack back to the main thread and ends.

the characteristics of the stack: advanced after the out. For example a method A calls itself, I use the numbering to distinguish the stack process:

a-> A (1)-> A (2)-> A (3)

when a (3) satisfies a condition that exits, returns a (2), A (2) ends back to a (1), and then returns to a, the stack process:

A (3)-> A (2)-> A (1)-> a

for recursion, there is also an image of the understanding that when I was a child at home there is a closet, both ends of the cabinet are glass, head into the cupboard to look at a mirror, you will see mirrors in the mirror, and then there are mirrors in the mirror, but with the characteristics of recursion is different from the mirror is not the end of the reflection, as long as the eyes can always see the bottom of the words .

after understanding the recursion, look back and see how to solve the problem of Hanoi in a recursive way.

Case 1-assuming there is only one plate, the number of plates N=1

There is only one step to move the 1th plate from a to C, so I can describe this step for comparison purposes:

Step plate numbering moves from pillar to pillar

1 1 A C

Case 2-if there are two plates, the number of plates N = 2

Step plate numbering moves from pillar to pillar

1 1 A B

2 2 A C

3 1 B C

Case 3-if there are three plates, the number of plates N = 3

Step plate numbering moves from pillar to pillar

1 1 A C

2 2 A B

3 1 C B

4 3 A C

5 1 B A

6 2 B C

7 1 A C

How to find out the regularity of plate movement.

The most important thing we need to do is to move the bottom plate from a to C forever .

look at the move from 1 plates to 3 plates, in the move record, when the plate number and the number of plates are the same, their steps are moved from a to C (see the Bold part), and the other steps are equivalent.

then observe the 第1-3 and 第5-7 steps in the 3rd case

The 第1-3 step is to move from a to B if we think of B as an end point, then the 第1-3 step here is exactly the same as the three steps in the 2nd case, which are moved by a pillar, and the 2nd case is compared to the following parentheses to indicate

1 1 A C (a-> B)

2 2 A B (a-> C)

3 1 c b (b-> c)

Summary: Turn plate B to c.

The第5-7 step is to move from B to C if we think of C as the end point, then the 5-7 steps here are the same as the above, and the three steps in the 2nd case are exactly the same. Compared to the 2nd case:

5 1 B A (a-> b)

6 2 B C (A-> C)

7 1 A C (B-> c)

Summary: Turn plate B to a

According to this demo, there are a few rules:

1. When the plate is only one, only one action moves from a to C to end.

2. When there are n dishes, the middle of the action is moved from A to C, so that the bottom of the nth plate moved complete

3. The middle action can be considered: moving from A to B

4. The middle action can be considered: moving from B to C

2,3,4 can be expressed as

1 1 A B

2 2 A C

3 1 B C

This structure has been repeated.

reprint: http://blog.csdn.net/yafei450225664/article/details/8647908

To sum up the above, the problem is simply to look at:




 

code written with Python3:





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.