[Python] Towers of Hanoi problem

According to legend, in ancient Indian Temple, there is a condition known as Tower of Hanoi (Hanoi) game. The game apparatus is in a copper plate, with three rods (numbered A, B, C), A bottom-up lever 64 is placed in descending order according to gold plate (see below). Goal of the game: A bar of gold plate to move all rods C, and still maintain the original order folded. Operation rules: a plate can only be moved, and the three bars is always maintained during the movement of the tape in the next, in the small cap, the dish can be placed during operation A, B, C any one bar.

 

(1) as an intermediary in the C drive, the rod 1 to A n-1 B pad to move lever;
(2) A rod remaining n-th disc moves lever C;
(3) A lever as an intermediary; the rod 1 B n-1 number of the disc to move the lever C.
Recursion:
. 1  # p6_9..py 
2  # Tower of Hanoi 
. 3  DEF Hanoi (n-, X, Y, Z):
 . 4      IF n-==. 1 :
 . 5          Print (X, ' -> ' , Z)
 . 6      the else :
 . 7          Hanoi ( . 1-n- , X, Z, Y)
 . 8          Print (X, ' -> ' , Z)
 . 9          Hanoi (. 1-n- , Y, X, Z)
 10 n-int = (iNPUT ( ' enter the Tower of Hanoi layers: ' ))
 . 11 Hanoi (n-, ' X- ' , ' the Y' , ' Z ' )

 

Guess you like

Origin www.cnblogs.com/zlc364624/p/11589742.html