First, the code Tower of Hanoi
= COUNT 0 DEF Hanoi (n-, the src, DST, MID): # define four parameters represent the number of disks, the source column, the target column, the middle column transition global COUNT # implemented using global variables in a global reserved word IF n-==. 1 : Print ( " STEP {}: {} -> {} " .format (. 1, the src, DST)) # If only one disc, the column was moved to the destination directly from the source to the column count + . 1 = the else : Hanoi (n- -1, the src, mID, DST) # the n-1 th column from the source disk to move the intermediate column Print ( " STEP {}: {} -> {} " .format (n-, the src, DST)) # the last column from the source disk to move the target column COUNT + =. 1 Hanoi (n- -1, MID, DST, the src) # the n-1 th column from the intermediate disc to a target column n-= int (INPUT ()) Hanoi (n-, " A " , " C " , " B " )
The results presentation
Second, the Tower of Hanoi animation
Code references https://blog.csdn.net/BeerBread134/article/details/69226991
The code can run up to 7-order Tower of Hanoi, mainly with the idea of recursion and stack, achieved with a turtle.
import turtle class Stack: def __init__(self): self.items = [] def isEmpty(self): return len(self.items) == 0 def push(self, item): self.items.append(item) def pop(self): return self.items.pop() def peek(self): if not self.isEmpty(): return self.items[len(self.items) - 1] def size(self): return len(self.items) defdrawpole_3 (): # Draw Tower of Hanoi Poles T = turtle.Turtle () t.hideturtle () DEF drawpole_1 (K): t.up () t.pensize ( 10 ) t.speed ( 100 ) T. Goto ( 400 * (. 1-K), 100 ) t.down () T. Goto ( 400 * (. 1-K), -100 ) T. Goto ( 400 * (. 1-K) -20, -100 ) T. GOTO ( 400 * (. 1-K) +20, -100 ) drawpole_1 (0) # draw Tower of Hanoi Poles [0] drawpole_1 (. 1) # draw Tower of Hanoi Poles [. 1] drawpole_1 (2) #Draw Tower of Hanoi Poles [2] DEF creat_plates (n): # producing n disks Plates = [turtle.Turtle () for I in Range (n)] for I in Range (n): Plates [I]. up () Plates [I] .hideturtle () Plates [I] .shape ( " Square " ) Plates [I] .shapesize ( 1,8 I) Plates [I] .goto ( -400, -90 * + 20 is I) Plates [I] .showturtle () return Plates DEF pole_stack (): # producing poles stack poles = [stack () for Iin Range (. 3 )] return Poles DEF moveDisk (Plates, Poles, FP, TP): # the poles [fp] to the top plate plates [mov] From poles [fp] to move Poles [TP] MOV = poles [fp] .peek () Plates [MOV] .goto ((FP -1) * 400,150 ) Plates [MOV] .goto ((TP -1) * 400,150 ) L = Poles [TP] .size () # determines the movement in the end portion height (on the plate just above the uppermost original) plates [MOV] .goto ((. 1-TP) * 400, * 20 is -90 + L) DEF moveTower (plates, Poles, height, fromPole, toPole, withPole): # recursive discharge tray IF height> =. 1 : moveTower (plates, Poles, height -1,fromPole,withPole,toPole) moveDisk(plates,poles,fromPole,toPole) poles[toPole].push(poles[fromPole].pop()) moveTower(plates,poles,height-1,withPole,toPole,fromPole) myscreen=turtle.Screen() drawpole_3() n=int(input("请输入汉诺塔的层数并回车:\n")) plates=creat_plates(n) poles=pole_stack() for i in range(n): poles[0].push(i) moveTower(plates,poles,n,0,2,1) myscreen.exitonclick()
The results show