Discuss / Python / 微末见解

微末见解

Topic source

よろしく

#1 Created at ... [Delete] [Delete and Lock User]
# 将A抽象为源柱,B抽象为辅助柱,C抽象为目标柱
def move(n, a, b ,c):
    if n > 0:
        # 将 n-1 个盘子从源柱移动到辅助柱
        move(n-1,a,c,b)
        # 将第 n 个盘子从源柱移动到目标柱
        print(a, '-->', c)
        # 将 n-1 个盘子从辅助柱移动到目标柱
        move(n-1,b,a,c)

move(3,'A','B','C')

关键在于将柱子抽象化,然后是盘子的部分整体化,不要去想具体的ABC柱子位置以及第几个盘子该怎么走。


  • 1

Reply