河内塔
代码如下:
#河内塔
def moveTower(height,fromPole,toPole,withPole):
if height>=1:
moveTower(height-1,fromPole,withPole,toPole)
moveDisk(fromPole,toPole)
moveTower(height-1,withPole,toPole,fromPole)
def moveDisk(fp,tp):
print("moving disk from",fp,"to",tp)
moveTower(4,"A","B","C")
运行结果为:
moving disk from A to C
moving disk from A to B
moving disk from C to B
moving disk from A to C
moving disk from B to A
moving disk from B to C
moving disk from A to C
moving disk from A to B
moving disk from C to B
moving disk from C to A
moving disk from B to A
moving disk from C to B
moving disk from A to C
moving disk from A to B
moving disk from C to B
网友评论