Hanoi塔递归,它在Python的每一步中都抛出了当前配置

问题描述

我必须为河内塔问题实施解决方案。我了解递归解决方案,但是我想定义一个函数(以(光盘数-整数)作为输入),并列出每次移动时的当前配置。

样本输出

Initial condition = [3,2,1] [] []
Step 1 = [3,2] [1] []
Step 2 = [3] [1] [2]
Step 3 = [3] [] [2,1]

我可以使用以下代码在每个步骤中打印移动说明:

def TowerOfHanoi(n,from_rod,to_rod,aux_rod):
    if n == 1:
        print("Move disk 1 from rod","to rod",to_rod)
        return
    TowerOfHanoi(n-1,aux_rod,to_rod)
    print("Move disk",n,"from rod",to_rod)
    TowerOfHanoi(n-1,from_rod)

如果我能以某种方式将(from_rod,to_rod)元组存储在某个列表的每个步骤中- moves 并使用 moves 作为另一个执行列出的动作的功能

def Perform_moves(moves,([5,4,3,1],[],[])):
  # Perform the sequence of moves on the current configuration which is second input in this function

我知道我的方法听起来很狡猾,但是有办法吗?

PS:另外,我该如何修改框架问题?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)