Let you really understand Hanruota problem in one minute - python implementation

#Question: Move the three blocks that were originally on disk A from A to B in turn, ensuring that the small disk is always on top of the large disk during the movement #ABC Three
disks
#mFrom The source is initially A disk
#mTo The destination is initial #mHelp for drive B
, initially for drive C
def printHanoiTower(N, mFrom, mTo, mHelp):

    if N == 1:
        #Export if N=1, recursive callback
        print("move", N, "from", mFrom, "to", mTo)
    else:
        #Move N-1 disks from the source to the auxiliary disk
        printHanoiTower(N-1, mFrom, mHelp, mTo)
        #Move N disk from source to destination
        print("move", N, "from", mFrom, "to", mTo)
        #Move N-1 Disk moved from auxiliary disk to destination
        printHanoiTower(N-1, mHelp, mTo, mFrom)
        
if __name__ == "__main__":
    printHanoiTower(10, "A", "B", "C")

Guess you like

Origin blog.csdn.net/qq_50709355/article/details/123346099