1.python算法之汉诺塔

代码如下:

#!/usr/bin/env python
# encoding: utf-8
"""
@author: 侠之大者kamil
@file: 汉诺塔.py
@time: 2016/3/20 20:00
"""
m = input(">>Please enter a maximum value of the sequence:")
m = int(m)+1
def move(a,b,c,n):
    if n ==1:
        print("%s ->> %s : %s" %(a[0],b[0],a[-1]))
        b.append(a.pop())
        print(x,y,z)
        return
    move(a,c,b,n-1)
    print("%s ->> %s : %s" %(a[0],b[0],a[-1]))
    b.append(a.pop())
    print(x,y,z)
    move(c,b,a,n-1)
x = ['x']
y = ['y']
z = ['z']
for n in range(1,m)[::-1]:
    x.append(n)
move(x,y,z,m-1)
print(x,y,z)

 

posted @ 2016-03-20 20:30  侠之大者kamil  阅读(180)  评论(0编辑  收藏  举报