#File Name : 汉诺塔问题.py
# from to help 三个杆子 1~N 在from上
# 先把1~N-1 从from到help上 借助了help 给最后的N腾出位置移动
# N移动到to上
# 1~N-1 再去to上
def process(n,from1,to1,help1):
if n==1 :
print('move 1 from '+from1+' to '+to1)
else:
process(n-1,from1,help1,to1)
print('move '+str(n)+' from '+from1+' to '+to1)
process(n-1,help1,to1,from1)
process(3,'左','右','中')
#时间复杂度 2**n-1
汉诺塔问题(python)
最新推荐文章于 2024-01-19 11:38:21 发布