Solving the problem of the Nottingham tower with recursion (recursion Hanoi tower Python)

Source: Internet
Author: User

The Hanoi tower problem stems from an ancient Indian legend: When Brahma created the world, he made three diamond pillars, and stacked 64 gold discs on a pillar from bottom to top in order of size. Brahma ordered the Brahman to rearrange the discs in order of size , and to specify that the discs should not be enlarged on the small discs , and that only one disc could be moved between the three pillars at a time . when all the golden discs are re-placed on another pillar, the world will perish in the sound of Thunder, and the Vatican, temples and sentient beings will perish.

Suppose a is the starting bar, B is the middle column, and C is the target column.

Start with the simplest example:

    • If there is only one disc left on the A post, move the disc from the A column to the C bar. (A--C)
    • If there are two discs left on the A column, first move the small disk from a to B, then move the large disk from the A to the C pillar, and finally the small disc on the B-pillar to the C-pillar. (A-to B, a-to C, B--and C)
    • If there are three discs left on column A, then move the smallest disk from the A to the C-pillar, then the intermediate-sized disk from the A-column to the B-column, then the smallest disk on the C-pillar to the B-pillar, then the A-column of the largest disk to the C-pillar, and then the B-pillar of the smallest disk to the A-column, Continue to move the middle-sized disc on the B-pillar to the C-bar and finally move the smallest disc on the A-column to the C-pillar. (A-->c, a-->b, C--and B, a-and C, B--A, B--and C, a--and C)---the first three steps (a-->c, a-->b, C-- b) can be seen as a-to-B process, the middle of a-to-c process, the last three steps (b--A, B--and C, a-and C) can be seen as a B-to-C process

In summary, if you need to move n disks, the entire process can be abstracted into the following three steps:

1. Move a disc other than the chassis (n-1 disc) from A to B column

2. Move the chassis from column A to column C

3. Move the disk (n-1 discs) on the B-pillar to the C-pillar

Start with the most complex examples:

    • If there are 64 discs on a column, the simplest way is to think of the 64 discs on the A column as a total of 2 discs (the chassis is a disc, the chassis above the 63 discs are a disk), so that the first column on the a bar to move to the B-column, and then the chassis from a column to the C column, Finally, the 63 discs on the B-pillar are moved to the C-pillar.
    • If there are 63 discs on the A column, the 63 discs on the A column are imagined to be 2 discs (the chassis is a disc, the 62 discs above the chassis are a disk), so that only the 62 discs on the A post are moved to the B-pillar, and then the chassis is moved from the A-column to the C-pillar, Finally, the 62 discs on the B-pillar are moved to the C-pillar.
    • And so on, until there is only one disk left on the A column, then move the disc from the A column to the C-bar.

As you can see, this problem can be solved by recursive methods by repeatedly nesting.

The code is as follows:
defHanoi (N,A,B,C):#n indicates the need to move several discs, a for the starting bar, b for the middle column, and C for the target column    ifN==1:#If there are only 1 discs left, move the C-pillar from the A-column to        PrintA" -", c)Else:#when n > 1 o'clock, move with an abstraction of 3 stepsHanoi (N-1,A,C,B)#Move the n-1 disc from A to B        PrintA" -"C#move the chassis from a to CHanoi (N-1,B,A,C)#Move the n-1 disc on B to C

Try to move the 3 discs with the same steps as before:

Hanoi (3,"A","B","C" )

The results of the operation are as follows:

A-C
A-B
C-B
A-C
B-A
B-C
A-C

As you can see, moving 3 discs takes 7 steps. According to the projections, moving N discs requires a 2n-1 step. Assuming that each time a disk is moved is 1 seconds, and the Brahman keeps moving the disk, the world will be destroyed by a total of (264-1) seconds. According to 365 days a year, it takes 584,942,417,355.072 years for the world to be destroyed.

Solving the problem of the Nottingham tower with recursion (recursion Hanoi tower Python)

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.