count = 0
def hanoi(n,A,B,C):
'''
算法①先将n-1个碟子借助C,从A移到B;
②将第n个碟子从A移到C;
③将n-1个碟子借助A,从B移到C;
'''
global count
'''
if n = 1 :
print("Move ", str(n)," from " ,A ," to ",C )
'''
if n ==0 :
return
else :
hanoi(n-1,A,C,B)
print("Move ", str(n)," from " ,A ," to ",C )
count+=1
hanoi(n-1,B,A,C)
#测试
hanoi(4,"Left","Mid","Right")
print(count)
汉诺塔 递归算法
最新推荐文章于 2025-06-01 14:22:54 发布