利用递归函数移动汉诺塔:
def move(n, a, b, c):
if n == 1:
print('move', a, '-->', c)
else:
move(n-1, a, c, b)
move(1, a, b, c)
move(n-1, b, a, c)
move(3, 'A', 'B', 'C')
打印移动过程
move A --> C
move A --> B
move C --> B
move A --> C
move B --> A
move B --> C
move A --> C
网友评论