【Python】汉诺塔问题

count = 0
def hannuota(n,src,dst,mid):      #n是圆盘数,src是起始,dst是目标,mid是过度
global count
if n == 1:
print('{}:{}->{}'.format(1,src,dst))  #当圆盘是1时,从起始柱子移到目标柱子
count += 1
else:
hannuota(n-1,src,mid,dst)      #剩余的圆盘从A移到B柱子
print('{}:{}->{}'.format(n,src,dst))  #最大的圆盘从A移到C柱子
count += 1  
hannuota(n-1,mid,dst,src)      #剩余的圆盘从B柱子到C柱子
hannuota(3,'A','C','B')
print(count)

#我现在理解的是->设想有1,2,3,4,5个圆盘,先把上面四个圆盘从A移到B柱子,然后把5从A移到C柱子,然后把1234从B移到C柱子,然后把这些动作分解成n=1。
#我也搞不懂,好难啊!

猜你喜欢

转载自www.cnblogs.com/naraka/p/8970903.html