[python]漢諾塔問題求解

towers of hanoi
漢諾塔問題:將A塔上的5個圓盤移動到C塔上
可以用遞歸的思路來求解該問題,爲了使問題一般化,假設要移動n個圓盤。根據遞歸的思路,可以考慮先把上面n-1個圓盤移動到B塔上,然後將最底下的n號圓盤移動到C塔,再將B塔上n-1個圓盤移動到C塔。
Python實現

#towers of hanoi 漢諾塔問題
def extList(L,length=5):
    if len(L) == length:
        return L[:]
    l = L[:]
    l.extend([0]*(length-len(l)))
    return l

def disState():
    ta = extList(TA,LENGTH)
    tb = extList(TB,LENGTH)
    tc = extList(TC,LENGTH)
    print 'A   B   C'
    for i in range(LENGTH-1,-1,-1):
        print '%d   %d  %d' % (ta[i],tb[i],tc[i])
    print '*' * 80

def move(N,frompeg,topeg,auxpeg):
    if N == 1:
        print 'move disk %d from %s to %s' % (N,frompeg,topeg)
        exec 'disk = T%s.pop()' % frompeg
        exec 'T%s.append(disk)' % topeg
        disState()
        return
    move(N-1,frompeg,auxpeg,topeg)
    print 'move disk %d from %s to %s' % (N,frompeg,topeg)
    exec 'disk = T%s.pop()' % frompeg
    exec 'T%s.append(disk)' % topeg
    disState()
    move(N-1,auxpeg,topeg,frompeg)

if __name__ == '__main__':
    #定義總共有多少個圓盤,這裏假設有5個圓盤
    LENGTH = 5
    #假設這5個圓盤一開始放在‘A’號塔上,擺放順序類似於堆棧,頂部存放1號圓盤,底部存放5號圓盤
    TA = [i for i in range(LENGTH,0,-1)]
    #‘B’號塔,初始化爲空
    TB = []
    #‘C’號塔,初始化爲空
    TC = []
    disState()
    #將‘A’號塔上的圓盤全部移動到‘C’號塔,‘B’號塔作爲中轉塔
    move(LENGTH,'A','C','B')

5個圓盤的輸出結果

>>> 
A   B   C
1   0   0
2   0   0
3   0   0
4   0   0
5   0   0
********************************************************************************
move disk 1 from A to C
A   B   C
0   0   0
2   0   0
3   0   0
4   0   0
5   0   1
********************************************************************************
move disk 2 from A to B
A   B   C
0   0   0
0   0   0
3   0   0
4   0   0
5   2   1
********************************************************************************
move disk 1 from C to B
A   B   C
0   0   0
0   0   0
3   0   0
4   1   0
5   2   0
********************************************************************************
move disk 3 from A to C
A   B   C
0   0   0
0   0   0
0   0   0
4   1   0
5   2   3
********************************************************************************
move disk 1 from B to A
A   B   C
0   0   0
0   0   0
1   0   0
4   0   0
5   2   3
********************************************************************************
move disk 2 from B to C
A   B   C
0   0   0
0   0   0
1   0   0
4   0   2
5   0   3
********************************************************************************
move disk 1 from A to C
A   B   C
0   0   0
0   0   0
0   0   1
4   0   2
5   0   3
********************************************************************************
move disk 4 from A to B
A   B   C
0   0   0
0   0   0
0   0   1
0   0   2
5   4   3
********************************************************************************
move disk 1 from C to B
A   B   C
0   0   0
0   0   0
0   0   0
0   1   2
5   4   3
********************************************************************************
move disk 2 from C to A
A   B   C
0   0   0
0   0   0
0   0   0
2   1   0
5   4   3
********************************************************************************
move disk 1 from B to A
A   B   C
0   0   0
0   0   0
1   0   0
2   0   0
5   4   3
********************************************************************************
move disk 3 from C to B
A   B   C
0   0   0
0   0   0
1   0   0
2   3   0
5   4   0
********************************************************************************
move disk 1 from A to C
A   B   C
0   0   0
0   0   0
0   0   0
2   3   0
5   4   1
********************************************************************************
move disk 2 from A to B
A   B   C
0   0   0
0   0   0
0   2   0
0   3   0
5   4   1
********************************************************************************
move disk 1 from C to B
A   B   C
0   0   0
0   1   0
0   2   0
0   3   0
5   4   0
********************************************************************************
move disk 5 from A to C
A   B   C
0   0   0
0   1   0
0   2   0
0   3   0
0   4   5
********************************************************************************
move disk 1 from B to A
A   B   C
0   0   0
0   0   0
0   2   0
0   3   0
1   4   5
********************************************************************************
move disk 2 from B to C
A   B   C
0   0   0
0   0   0
0   0   0
0   3   2
1   4   5
********************************************************************************
move disk 1 from A to C
A   B   C
0   0   0
0   0   0
0   0   1
0   3   2
0   4   5
********************************************************************************
move disk 3 from B to A
A   B   C
0   0   0
0   0   0
0   0   1
0   0   2
3   4   5
********************************************************************************
move disk 1 from C to B
A   B   C
0   0   0
0   0   0
0   0   0
0   1   2
3   4   5
********************************************************************************
move disk 2 from C to A
A   B   C
0   0   0
0   0   0
0   0   0
2   1   0
3   4   5
********************************************************************************
move disk 1 from B to A
A   B   C
0   0   0
0   0   0
1   0   0
2   0   0
3   4   5
********************************************************************************
move disk 4 from B to C
A   B   C
0   0   0
0   0   0
1   0   0
2   0   4
3   0   5
********************************************************************************
move disk 1 from A to C
A   B   C
0   0   0
0   0   0
0   0   1
2   0   4
3   0   5
********************************************************************************
move disk 2 from A to B
A   B   C
0   0   0
0   0   0
0   0   1
0   0   4
3   2   5
********************************************************************************
move disk 1 from C to B
A   B   C
0   0   0
0   0   0
0   0   0
0   1   4
3   2   5
********************************************************************************
move disk 3 from A to C
A   B   C
0   0   0
0   0   0
0   0   3
0   1   4
0   2   5
********************************************************************************
move disk 1 from B to A
A   B   C
0   0   0
0   0   0
0   0   3
0   0   4
1   2   5
********************************************************************************
move disk 2 from B to C
A   B   C
0   0   0
0   0   2
0   0   3
0   0   4
1   0   5
********************************************************************************
move disk 1 from A to C
A   B   C
0   0   1
0   0   2
0   0   3
0   0   4
0   0   5
********************************************************************************
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章