Tower of Hanoi

1. Tower of Hanoi  

  The Tower of Hanoi (Hong Kong and Taiwan: Tower of Hanoi ) is a mathematical problem formed according to a legend:

  There are three poles A, B, C. There are N (N>1) perforated disks on the A rod, and the size of the disks decreases from bottom to top. All discs are required to be moved to the C-bar according to the following rules:

    1.   Only one disc can be moved at a time;
    2.   Large plates cannot be stacked on top of small plates.

  Tip: The disc can be temporarily placed on the B rod, or the disc removed from the A rod can be moved back to the A rod, but the above two rules must be followed.

  Q: How to move? How many moves are required at least?

 

2. Use recursive solution

  Python:

def move(n, a = "A", b="B", c="C"): 
if not isinstance(n, (int)): #Determine whether it is an integer
print("The number of plates is a positive integer!" )
return
if n <= 0:
print("The number of plates is a positive integer!") #
return
elif n == 1:
print(a, "-->", c)
else:
move(n-1, a, c, b) #Move more than 2 layers on the a column to the b column
move(1, a, b, c) #Move the bottom layer on the a column to the c column
move(n-1 , a, b, c) #Move all the plates on the b column to the c column

if __name__ == '__main__':
move(4, "A", "B", "C")
print("== ============")
move(5.5)
print("==============")
print("Please input the number of plates:")
n = input()
move(int(n))

  Java:

public class Test {

    // Tower of Hanoi recursion
    public static void move(int n, String a, String b, String c){
        if (n <= 0){
            System.out.println("The number of plates is a positive integer!");
            return;
        }else if (n == 1){
            System.out.println(a + "-->" + c);
        }else {
            move(n-1, a, c, b);
            move(1, a, b, c);
            move(n-1, b,a ,c);
        }
    }

    public static void main(String[] args) {
        move(3, "A", "B", "C");
    }

}

 

3. Understanding of recursion in the Tower of Hanoi game (Knowing God is everywhere)

Author: Fireman A
Link: https://www.zhihu.com/question/24385418/answer/257751077
Source: Zhihu The
copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.

The point of understanding recursion is mainly to give up!

Abandon your attempts to understand and track the entire recursion and only understand the transition between the two levels of recursion, and the conditions under which the recursion terminates.

Imagine you are in a tropical jungle and accidentally discover the 10-story Tower of Hanoi. Just when you were thinking about how to move it, a native came out of the forest and offered to help you move the tower. His name is Ersha, and he wears a straw hat with the word 2 on it. He claims to move the first and second disks to any post.

You had an idea and asked, "Don't you have a brother named Sansha?"
"Yes, sir, how did you know? He will move the first to the third plate."
"Then you go and call him, I don't need you anymore."
So Sansha came, and he also wore a straw hat with the word 3 on it.

You said: "San Sil, help me move the first three dishes to the C-pillar."
San Si pondered for a while, and walked into the woods, you heard him yell: "Er Sil, come out and help me move the first two dishes." to C!"

You start dozing off because of the heat. In the haze, you didn't see how Ersha worked. After Ersha finished, he walked into the forest and shouted, "Old third, I'm done!"

San Sil came out, moved the third plate from A to B, and then called Er Sil again: "Second, help me move the first two plates back to A!"

I won't say much about the rest. In a word, Sansha actually only moved the No. 3 disk, and the others called Ersha to work. The last step is to move the third disk to C, then call the second silly to move the first two disks back to C.

After the matter is over, you call Sansha and say to him, "Actually, you don't know how to move the three plates to C step by step, do you?"

San said inexplicably, "Didn't I finish the task?"

You said, "But you actually asked your brother Er silly to do most of the work?"

Sansha said, "I outsourced it to him and it has nothing to do with you?"

You asked: "Did Ersha also outsource to anyone?"

San smirked: "This has nothing to do with me?"

You thought hard all night, and the next day, you walked into the forest and shouted, "Ten fools, where are you?"

A man with a No. 10 straw hat on his head, ten stupid, said: "Master, what's the matter with you?"

"I want you to help move plates 1 to 10 to the C-pillar"

"Okay, sir." Shisha turned around and walked towards Linnei.

"Wait, why don't you go back and call your brother Jiu Shi?"

"How did you know, sir?"

"So you ordered him to move the first nine trays over and over, you just need to move the tenth tray a few times, right?"

"Yes!"

"Do you know how he does it?"

"Does this have anything to do with my fart?"

You sighed and decided to give up. The ten fools started to work. The woods were filled with shouts one after another: "Nine fools, come on!" "Old eight, it's your turn!" "Five fools!..." "Three fools!..." "Big fool!"

You notice that Dasha never calls anyone, but Dasha's job is also the easiest, he just moves the first plate around.

After a few years, the work is over. Ten fools came to you. You ask the ten fools: "Who taught you to work like this?"

Ten fools said, "My dad. He left me this note."

He took a small note from his pocket that read: "Move the plate according to the number of your hat to the goal post. If a plate overwhelms you, ask one of your older brothers to remove him. Hold the post you're going to, and ask your brother to move it out of the way. When your plate has moved to the target, ask your brother to move the plate that should be on top of you back on top of you."

You asked inexplicably, "What would the idiot do if he didn't have a brother?"

Ten smirked: "He only cares about the first plate, so he will never encounter those two 'ifs', and there is no plate that should be placed on the first plate."

But at this time he suddenly changed color, as if revealing a huge secret. He gave you a panicked look and ran quickly into the woods.

The next day, you go into the woods to search for the ten brothers. They have disappeared. You have found a hut for one person only, but there are ten straw hats with numbers one to ten written on them.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325259861&siteId=291194637