【Python】汉诺塔问题

 

count=0
def hanoi(n,src,dst,mid):  #n为圆盘数量,src表示源柱子,dst表示目标柱子,mid表示中间过渡柱子
    global count #定义全局变量
    if n==1:
        print("{}:{}->{}".format(1,src,dst))
        count+=1
    else:
        hanoi(n-1,src,mid,dst)
        print("{}:{}->{}".format(n,src,dst))
        count+=1
        hanoi(n-1,mid,dst,src)
hanoi(3,"A","C","B") #有三个圆盘
print(count)
    

搬运过程:

原文地址:https://www.cnblogs.com/HGNET/p/12591765.html