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