The solution to the Tower of Hanoi algorithm problem (Java), ideas and inferences

First of all, put the code first, explanations and comments will be written separately later

public class hnt {
	public static void main(String[] args) {
		hnts("a","b","c",3);
	}
	
	public static void hnts(String from,String temp,String to,int n){
		if(n==1){
			System.out.println(from+"------>"+to);
		}else{
			hnts(from,to,temp,n-1);
			hnts(from,temp,to,1);
			hnts(temp,from,to,n-1);
		}
		
	}
}

Original problem description (source Baidu Encyclopedia)

According to legend, in the ancient Indian temple, there is a game called Hanoi Tower (Hanoi). The game is on a copper plate device with three pillars (numbered A, B, C), and 64 gold discs are placed in order on the A pole from top to bottom, from large to small (as shown in the figure below). The goal of the game: Move all the gold discs on the A pole to the C pole, and keep the original order. Operating rules: Only one plate can be moved at a time, and the three rods should always keep the large plate on the bottom and the small plate on the top during the movement process. The plate can be placed on any rod A, B, or C during the operation.


Problem solving ideas

    First of all, let's unify the environmental assumptions. When a Tower of Hanoi is placed in front of us, we determine that a certain disk is (any column) counted from the top to the bottom, and we will give this disk. set a number as n

    Then set a name for the three pillars based on the perspective of the disk, namely "from" where the plate is currently located, "to" where the plate wants to reach, and the other column "temp" that can be used as a temporary foothold. "

    

    The Tower of Hanoi Law can be summed up in four steps:

            One: When the disc (n) is on the top layer, that is, when n==1, the disc can be moved directly from from to to     

            System.out.println(from+"------>"+to);
            Two: When this disc (n) is not on the top layer, that is, when there is a disc on it, we need to move the disc (n-1) above it to temp first

            hnts(from,to,temp,n-1);

            Three: When the disc (n) moves to temp, at this time, the disc n will become n-(n-1)=1, so the disc n (now 1) at this time can be directly moved to to

	    hnts(from,temp,to,1);
            Four: When the disc n (1), after moving to to, we need to move (n-1) on temp to to

	    hnts(temp,from,to,n-1);

summary and inference

These four steps are actually the rules we have concluded based on certain experience and attempts.

The thinking line of this question can be summarized into the following parts. Summarizing the thinking line can be used to help us face the same problem in the future, and draw inferences from one case:

        First of all, when we see this problem, we can simply bring in 1 or 2 plates to try. When we try to have 3 plates, we should think about what method can be used to solve this problem. Question, after trying 1, 2, and 3 plates, we can roughly find that this movement is a little regular. Well, then we can basically guess that we can try it with a loop or a recursion, but obviously if we use a loop, The loop jump control of the plate is too weak, so you can choose to use recursion, if not, try to use loop again.

        Now that we choose to use recursion, we start to think about how to find this law.

        The general concept of recursive problem solving can be roughly understood as the refrigerator enlarges the elephant: open the refrigerator door, put the elephant in, and close the refrigerator door. But how the process is plugged in and how to close the door, we don't need to think too much, we only need to give it a start (parameter transmission) and an end (exit). So at this point we can find the four solution steps as described above. Using these four steps we can find the solution to the problem.


Guess you like

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