. 1 COUNT = 0 2 DEF Hanoi (n-, src, dst, mid): # n-represents the number of disks, src denotes a source column, dst indicates the purpose of the column, mid column represents a transition . 3 Global COUNT # global variable defined, the function internal global reserved word . 4 IF n-==. 1 : . 5 Print ( " {}: {} -> {} " .format (. 1 , the src, DST)) . 6 COUNT + =. 1 . 7 the else : # the n-1 th disc on the mid, the last disc is placed on dst . 8 Hanoi (. 1-n-, the src, mid, dst) # called twice . 9 Print ( " {}: {} -> {} ".format(n,src,dst)) 10 count +=1 11 hanoi(n-1,mid,dst,src) 12 hanoi(3,"A","C","B") 13 print(count)
The output is:
1:A->C
2:A->B
1:C->B
3:A->C
1:B->A
2:B->C
1:A->C
7