河内塔问题

河内塔问题_第1张图片


河内塔问题_第2张图片

def  tower(height,fromtw,totw,midtw):

   if height==1:

print"{}-->{}".format(fromtw,totw)

else:

tower(height-1,fromtw,midtw,totw)

print“{}-->{}”.format(fromtw,totw)

tower(height-1,midtw,totw,fromtw)

tower(2,"A","B","C")

A-->C

A-->B

C-->B

你可能感兴趣的:(河内塔问题)