2019-08-19 21:37:49 +08:00
|
|
|
def moveTower(height, fromPole, toPole, withPole):
|
2019-10-05 13:14:13 +08:00
|
|
|
"""
|
2016-08-14 20:13:32 +08:00
|
|
|
>>> moveTower(3, 'A', 'B', 'C')
|
|
|
|
moving disk from A to B
|
|
|
|
moving disk from A to C
|
|
|
|
moving disk from B to C
|
|
|
|
moving disk from A to B
|
|
|
|
moving disk from C to A
|
|
|
|
moving disk from C to B
|
|
|
|
moving disk from A to B
|
2019-10-05 13:14:13 +08:00
|
|
|
"""
|
2016-08-14 20:13:32 +08:00
|
|
|
if height >= 1:
|
2019-10-05 13:14:13 +08:00
|
|
|
moveTower(height - 1, fromPole, withPole, toPole)
|
2016-08-14 20:13:32 +08:00
|
|
|
moveDisk(fromPole, toPole)
|
2019-10-05 13:14:13 +08:00
|
|
|
moveTower(height - 1, withPole, toPole, fromPole)
|
|
|
|
|
|
|
|
|
|
|
|
def moveDisk(fp, tp):
|
|
|
|
print("moving disk from", fp, "to", tp)
|
2016-08-14 20:13:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2019-10-05 13:14:13 +08:00
|
|
|
height = int(input("Height of hanoi: ").strip())
|
|
|
|
moveTower(height, "A", "B", "C")
|
|
|
|
|
2016-08-14 20:13:32 +08:00
|
|
|
|
2019-10-05 13:14:13 +08:00
|
|
|
if __name__ == "__main__":
|
2016-08-14 20:13:32 +08:00
|
|
|
main()
|