Python实现汉诺塔(hanoi)列表的转移

汉诺塔不必多说,常用的实现方式——递归 也不用多说,直接上代码:

__author__ = "Jazzon"
__coding__ = "UTF-8"
__version__ = "python3.x"
__contact__ = "1299277143@qq.com"

A = [6, 5, 4, 3, 2, 1]
B = []
C = []
n = len(A)


def move(n, A, B, C):
    if n == 1:
        C.append(A.pop())
    else:
        move(n - 1, A, C, B)
        C.append(A.pop())
        move(n - 1, B, A, C)


def main():
    print("移动前:")
    print('A = ', A, '\n', 'C = ', C, sep='')
    move(n, A, B, C)
    print("移动后:")
    print('A = ', A, '\n', 'C = ', C, sep='')


if __name__ == '__main__':
    main()

 

    原文作者: 汉诺塔问题
    原文地址: https://blog.csdn.net/a123gsfd/article/details/83822771
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞