有n个盘子在a上面,最终目的全移到c上面
1.先将最上面n-1个放入b
2.再将最下面一个放入c
3.将b上面的放入c
代码:
move(3, 'A', 'B', 'C')
def move(n,a,b,c)
if n<=0:
print('error')
elif n == 1:
print(a,'-->',c)
else:
move(n-1,a,c,b)
move(1,a,b,c)
move(n-1,b,a,c)
return
move(3,"A","B","C")
运行结果:
A --> C
A --> B
C --> B
A --> C
B --> A
B --> C
A --> C