Discuss / Python / 汉诺塔练习

汉诺塔练习

Topic source

520bv

#1 Created at ... [Delete] [Delete and Lock User]
#汉诺塔实现
def hano_tower(n,s='A',c='B',d='C'):  #n为汉诺塔盘数,s为起始柱,c为中转柱,d为目标柱
    if n == 1:
        print(s,'-->',d)
        return None
    hano_tower(n-1,'A','C','B')  #将上面n-1个盘子从A柱移到B柱
    print(s,'-->',d)             #将最底下的盘子从A柱移到C柱
    hano_tower(n-1,'B','A','C')  #将n-1个盘子从B柱移到C柱

步数统计没做,也没什么好做的就是2^n-1


  • 1

Reply