Try to understand the recursive algorithm through the Tower of Hanoi (Hanoi Tower)-Python

Foreword: I have never been able to understand this problem before, but today I suddenly realized it, and I figured it out, so I made a record.

If there is a misunderstanding, please correct me in the comment area.

Problem Description

There are three adjacent pillars, labeled A, B, and C. On pillar A, there are n disks of different sizes stacked in a pyramid shape from bottom to top. Move all the disks to pillar B one by one, and each The large plate cannot appear above the small plate on the same pillar every time it is moved. How should it be moved?
insert image description here

code:

def Move(n,start,middle,target):
    if n==1:
        print(start,'->',target)
        return 
    Move(n-1,start,target,middle)
    print(start,'->',target)
    Move(n-1,middle,start,target)
Move(3,A,B,C)

train of thought

The idea of ​​this topic is the recursive algorithm.
The core of recursion lies in a kind of thinking of recursion. Don’t always try to drill into recursion to think about how to recursively change variables layer by layer . Your little brain is not that big. Stack area! ! !

1. First, give some definitions:
we define the number of discs that need to be transferred as - n ;
all the discs in our current stage are placed on a column, we call this column - start , so the starting time A =start ;
our purpose is to transfer all of them to another column, we call this column - target , so the starting time C=target ;
at the same time, there is still the undefined column, we might as well Call it - middle , so the starting moment B=middle ;

2. We want to move n disks from start=A to its target=C, first we should move n-1 disks from start=A to middle=B, then for n-1 disks language, their target=B;
insert image description here

Then how can we move n-1 discs from all to B, that is, first move n-2 discs from start=A to target=C.
By analogy, until n=1, this time directly move this disc from its start to target.
In short,
when moving n disks, it has start, middle, and target;
at this time, it is necessary to move n-1 disks first, its start is still start, and its target is the last middle.
In code it corresponds to:

def Move(n,start,middle,target):
    if n==1:
        print(start,'->',target)
        return 
    Move(n-1,start,target,middle) 

Now we have moved n-1 discs from all to B, that is to say, all the recursion is over, and we are back to the current layer, remember the current layer? The current layer start=A, target=C. All we have to do next is to move 1 disc from A to C.
insert image description here

The corresponding code is one more line:

def Move(n,start,middle,target):
    if n==1:
        print(start,'->',target)
        return 
    Move(n-1,start,target,middle)
    print(start,'->',target)

This time is not over yet. On the n=n layer, our n-1 discs are all on middle=B. We should move them from middle=B to target=C. How to move them? With the help of A , In other words, for the n=n-1 layer, now the start is the middle in the n=n layer, the middle is the start in the n=n layer, and the target is the target in the n=n layer.
In code terms:

def Move(n,start,middle,target):
    if n==1:
        print(start,'->',target)
        return 
    Move(n-1,start,target,middle)
    print(start,'->',target)
    Move(n-1,middle,start,target)

Finally, I will make a small summary. In short, each layer of recursion must use the middle of the previous layer of recursion as the target of this layer,
and then move the start to the target when the recursion ends and returns to the current layer.
After the movement is over, because n-1 discs are placed on the middle, it is necessary to use the middle as the starting point at this time to move these discs to the target.

Guess you like

Origin blog.csdn.net/qq_49030008/article/details/124898077