汉诺塔

def hanoi(n, source, target, auxiliary):
    if n > 0:
        # 将 n-1 个盘子从源柱子移动到辅助柱子
        hanoi(n-1, source, auxiliary, target)
        # 将最大的盘子从源柱子移动到目标柱子
        print("Move disk", n, "from", source, "to", target)
        # 将 n-1 个盘子从辅助柱子移动到目标柱子
        hanoi(n-1, auxiliary, target, source)

if __name__ == "__main__":
    n = int(input("Enter the number of disks: "))
    hanoi(n, 'A', 'C', 'B')

 

posted @ 2023-07-08 17:25  胖豆芽  阅读(6)  评论(0编辑  收藏  举报