递归实现汉诺塔
参考实现
# 递归 -汉诺塔 def hanoi(n, a, b, c): if n > 0: hanoi(n - 1, a, c, b) print(f'moving from {a} --> {c}') hanoi(n - 1, b, a, c) hanoi(3, 'A', 'B', 'C')
运行效果
''' moving from A --> C moving from A --> B moving from C --> B moving from A --> C moving from B --> A moving from B --> C moving from A --> C '''