Python function Recursive Nottingham Tower

Source: Internet
Author: User

Hanoi: Hanoi (also known as Hanoi) is a puzzle toy from an ancient Indian legend. When great Brahma created the world, he made three diamond pillars,

The 64 gold discs are stacked on a pillar from bottom to top in order of size. The great Brahma commanded the Brahman to rearrange the discs from below to the other pillars in order of size.

It is also stipulated that the disc cannot be enlarged on the small disc, and only one disc can be moved between the three pillars at a time.

Hanoi movement problem, the Hanoi of the three cylinders are labeled A, B, C, the disc began to be on a cylinder above and on the bottom up in accordance with the order of the size of the disk, need to move it to

C-Cylinders, the idea of recursion is:

(1) A on the number of disks n = 1, then very direct, directly to C can be, that is a--c,n = 2 o'clock, also only need 3 steps;

(2) A on the number of disks n >= 3, the idea is to move the first with B, the (n-1) disk to B, and then a above the last disc moved to C,

(3) B above has (n-1) a disk, to move to C, according to the previous step of thought, must again use a to achieve

(4) When there are 1 plates, a (1)--C

(5) When there are n plates, a (n-1)--B, a (1)--C, B (n-1)--C, the idea of recursion,

1 def move (N, a, B, c):2     ifn = =1:3Print'%s to%s'% (A, c)) # There are only 1 plates, a--andC4     Else:5Move (n1, A, C, b) # There are n plates, with the aid of C,a (N-1)--B6Move1, A, B, c) # There are n plates, a (1)--C7Move (n1, B, A, c) # There are n plates, with the help of a B (n1)--C

Python function Recursive Nottingham Tower

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.