Python中河内变体的递归塔

Python中河内变体的递归塔,python,python-3.x,algorithm,recursion,towers-of-hanoi,Python,Python 3.x,Algorithm,Recursion,Towers Of Hanoi,我正在实现一个递归解决方案,它是典型的Hanoi Towers问题的一个变体,在这个问题中,为了解决这个问题,只能在相邻的销钉之间移动磁盘。我的近似值如下: 我知道这需要一个序列,其中最小的磁盘先向右移动两次,然后第二个磁盘移动一次,第一个磁盘返回到左侧,并进行移动。。。但是对于这个实现,它只在第一次迭代时起作用。有什么想法吗 提前谢谢 < P>编辑:2个初始盘的预期输出(我认为是基本情况)必须是: 我得到的是: 在这种限制下,宇宙将看到更长的生命: pa e bbb aa aa Gcccccc

我正在实现一个递归解决方案,它是典型的Hanoi Towers问题的一个变体,在这个问题中,为了解决这个问题,只能在相邻的销钉之间移动磁盘。我的近似值如下:

我知道这需要一个序列,其中最小的磁盘先向右移动两次,然后第二个磁盘移动一次,第一个磁盘返回到左侧,并进行移动。。。但是对于这个实现,它只在第一次迭代时起作用。有什么想法吗

提前谢谢

< P>编辑:2个初始盘的预期输出(我认为是基本情况)必须是:

我得到的是:


在这种限制下,宇宙将看到更长的生命:

pa
e bbb aa aa
Gccccccccccaabbbbaaaa
1滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴滴
通讯社
e a bb
g a bbb a a bbb a ccccc
2 a bbb a CCCCCCC a bbb a DDDDDD
P
e aa
g aa aa BBBB aa
3 aa BBBB aa CCCCCCCCCCCCCCAA
非递归过程:
第一步别无选择。
在以后可能的两个移动中,选择一个不撤消前一个移动

递归过程:

"""
Towers of Hanoi.
Moves restricted to neighbouring peg
implement pegs begin, mid, end and procedure move() to meet your needs
"""
# loads of empty lines for spoiling



























def towers_of__H_a_n_o_i(d, source=begin, destination=end, other=mid):
    """
    Towers of Hanoi.
    Moves restricted to neighbouring peg
    """
    if d < 1: return  
    if mid == other:
        towers_of__H_a_n_o_i(d-1, source, destination)
        move(source, other)
        towers_of__H_a_n_o_i(d-1, destination, source)
        move(other, destination)
        towers_of__H_a_n_o_i(d-1, source, destination)
    else:
        towers_of__H_a_n_o_i(d-1, source, other, destination)
        move(source, destination)
        towers_of__H_a_n_o_i(d-1, other, destination, source)
“”“
河内塔。
移动仅限于相邻的桩
执行PEG begin、mid、end和procedure move()以满足您的需要
"""
#大量的空线路被破坏
def towers_of_uuh_a_n_o_i(d,source=begin,destination=end,other=mid):
"""
河内塔。
移动仅限于相邻的桩
"""
如果d<1:返回
如果mid==其他:
塔楼(d-1,来源,目的地)
移动(源、其他)
塔楼(d-1,目的地,来源)
移动(其他,目的地)
塔楼(d-1,来源,目的地)
其他:
塔楼(d-1,来源,其他,目的地)
移动(源、目标)
塔楼(d-1,其他,目的地,来源)

当最初的左塔有4张光盘时,你能给出预期的完整输出吗?@trincot我编辑了添加2张光盘的问题(因为有4张光盘会更长)。谢谢,但我真的很想看到你用4张光盘完成这项工作,因为这似乎是不可能的。另外,在你的例子中,只有两张碟片,我不明白为什么最后一步是这样做的?在那次移动之前,目标不是已经实现了吗?为什么在
else
分支中,PEG的默认值总是正确的?为什么,当ode调用2-print基本情况和3个以上的打印时,你会期望8个吗?@greybeard这是我的问题,我不知道如何在算法中添加功能,以便移动到我知道的左侧。mid==其他分支看起来很奇怪。
Move 1 from peg 1 to 2 
Move 1 from peg 2 to 3 
Move 2 from peg 1 to 2 
Move 1 from peg 3 to 2 
Move 1 from peg 2 to 1 
Move 2 from peg 2 to 3 
Move 1 from peg 1 to 2 
Move 1 from peg 2 to 3
Move 1 from peg 1 to 2
Move 1 from peg 2 to 3
Move 2 from peg 1 to 2
Move 1 from peg 3 to 2
Move 1 from peg 2 to 1
"""
Towers of Hanoi.
Moves restricted to neighbouring peg
implement pegs begin, mid, end and procedure move() to meet your needs
"""
# loads of empty lines for spoiling



























def towers_of__H_a_n_o_i(d, source=begin, destination=end, other=mid):
    """
    Towers of Hanoi.
    Moves restricted to neighbouring peg
    """
    if d < 1: return  
    if mid == other:
        towers_of__H_a_n_o_i(d-1, source, destination)
        move(source, other)
        towers_of__H_a_n_o_i(d-1, destination, source)
        move(other, destination)
        towers_of__H_a_n_o_i(d-1, source, destination)
    else:
        towers_of__H_a_n_o_i(d-1, source, other, destination)
        move(source, destination)
        towers_of__H_a_n_o_i(d-1, other, destination, source)