主要使用函数的递归方法,考虑过程如下:
n,a,b,c(n代表罗汉塔块数,a,b,c代表三块柱子)
若n=1时,只需从a》》》c
若n>1时,需要把上面n-1块从a移动到b,底下1块从a移动到c,再把b上n-1移动到c

函数实现如下:
def move(n,a,b,c):
if n==1:
print(a,'>>>',c)
else:
move(n-1,a,c,b)
move(1,a,b,c)
move(n-1,b,a,c)
print(move(3,'a','b','c'))

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-19
  • 2021-07-12
  • 2021-06-10
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-10-12
  • 2021-06-10
  • 2021-12-20
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案