
Python hanoi 汉诺塔
Python 汉诺塔问题解法
发布日期:2021-05-18 13:16:02
浏览次数:15
分类:精选文章
本文共 372 字,大约阅读时间需要 1 分钟。
Python 汉诺塔问题
def hanoi(n, src, dest, help): """用递归的方式移动n个盘子从src到dest,辅助塔是help.""" if n == 1: print(f"{src} -> {dest}") else: # 首先移动n-1个盘子从src到help,借助dest hanoi(n-1, src, help, dest) print(f"{src} -> {dest}") # 然后将help上的n-1个盘子移动到dest,借助src hanoi(n-1, help, dest, src)