兄弟萌,话不多说,show me the code!
def moveTower(height,fromPole,withPole,toPole):
if height >= 1:
moveTower(height-1,fromPole,toPole,withPole)
moveDisk(height,fromPole,toPole)
moveTower(height-1,withPole,fromPole,toPole)
def moveDisk(disk,fromPolem,toPole):
print(f"Moving disk [{disk}] from {fromPolem} to {toPole}")
if __name__ == '__main__':
moveTower(5,"a","b","c")
输出:
Moving disk [1] from a to c
Moving disk [2] from a to b
Moving disk [1] from c to b
Moving disk [3] from a to c
Moving disk [1] from b to a
Moving disk [2] from b to c
Moving disk [1] from a to c
Moving disk [4] from a to b
Moving disk [1] from c to b
Moving disk [2] from c to a
Moving disk [1] from b to a
Moving disk [3] from c to b
Moving disk [1] from a to c
Moving disk [2] from a to b
Moving disk [1] from c to b
Moving disk [5] from a to c
Moving disk [1] from b to a
Moving disk [2] from b to c
Moving disk [1] from a to c
Moving disk [3] from b to a
Moving disk [1] from c to b
Moving disk [2] from c to a
Moving disk [1] from b to a
Moving disk [4] from b to c
Moving disk [1] from a to c
Moving disk [2] from a to b
Moving disk [1] from c to b
Moving disk [3] from a to c
Moving disk [1] from b to a
Moving disk [2] from b to c
Moving disk [1] from a to c