汉诺塔问题
规定:规在小圆盘上不能放大圆盘,在三根柱子之间一次只能移动一个圆盘
本质:经典递归问题,从第n个盘开始分析,先将最大圆盘移至目标处,再依次处理接下来每一个圆盘。

简单汉诺塔问题实例(三个圆盘)
代码如下~
count=0 def hanoi(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)
结果


浙公网安备 33010602011771号