Detailed explanation of the N-layer Tower of Hanoi (Hanoi) problem (java implementation)

Towers of Hanoi problem

According to legend, in a holy temple in ancient India, there was a game called Tower of Hanoi. The game is played on a copper plate device with three poles A, B, and C. Initially, plates are placed on pole A from small to large. The purpose and rules of the game are to move all the gold discs on pole A to pole C, and keep the original order. Operating rules: Only one plate can be moved at a time, and the large plate is always on the bottom and the small plate is on the top of the three rods during the movement process. The plate can be on any of the rods A, B, and C during the movement.

insert image description here

Solutions

The key idea to solve the problems related to the Tower of Hanoi is the idea of ​​freeing up space .
Taking the three-story Tower of Hanoi as an example, name the plates Plate 1, Plate 2, and Plate 3 from top to bottom, and take the left column, middle column, and right column from left to right.
insert image description here
Due to the rules of the Tower of Hanoi, we cannot directly move plate 1, plate 2, and plate 3 from the left column to the right column, so we have to split the steps.

Let the three plates move from the left column to the right column. The overall steps can be split into:

1. Disk 1 and Disk 2 move from the left column to the middle column (leftToMid).
insert image description here

2. Disk 3 moves from the left column to the right column (leftToRight).
insert image description here

3. Disk 1 and disk 2 move from the middle column to the right column (midToRight).
insert image description here

However, because the Tower of Hanoi problem has the rule that the big plate is always on the bottom and the small plate is on the top of the three poles during the movement process, all the above overall steps 1 and 3 cannot be realized in one step, so we continue to split the steps .

Step 1. Disk 1, disk 2 moves from the left column to the right column. The overall steps can be split into:

  1. Disk 1 moves from the left column to the right column (leftToRight).
    insert image description here

  2. Disc 2 moves from the left post to the middle post (leftToMid).
    insert image description here

  3. Disk 1 moves from the right column to the left column (rightToLeft).
    insert image description here

Step 1. Disk 1 and disk 2 move from the middle column to the right column. The overall steps can be divided into:

  1. Disk 1 is moved from the center post to the left post ( midToLeft ).
    insert image description here

  2. Disk 2 is moved from the center post to the right post ( midToRight ).
    insert image description here

  3. Disk 1 moves from the left column to the right column (leftToRight).
    insert image description here

It can be seen that in the process of moving the 3 plates from the left column to the right column, if you want the plate 3 to move from the left column to the right column, you must make room for plate 1 and plate 2 to move from the left column to the middle column , in the same way, if you want plate 1 and plate 2 to move from the center post to the right post, you must make room for plate 1 to move plate 1 from the center post to the left post.

It can be concluded that if you want N plates to move from left to right column, it can be decomposed into: N-1 plates move from the left column to the middle column, and the Nth plate moves from the left column to the right column, N -1 plate goes from the center post to the right post. In the same way, step N-1 where the plate moves from the left column to the middle column can be decomposed, and step N-1 plate can be decomposed into steps similar to the above from the center column to the right column.

Therefore, we can get the result of the Tower of Hanoi problem by continuously decomposing it into small steps. There are only 6 kinds (leftToRight, midToRight, leftToMid, midToLeft, rightToMid, rightToLeft), then we print the entire process of moving the n-layer Tower of Hanoi from the leftmost to the rightmost. The first code representation can be realized directly in these 6 ways .

Solution one

public static void Hanoi1(int n) {
    
    
       leftToRight(n);
    }

    private static void leftToRight(int n) {
    
    
        if (n==1) {
    
    
            System.out.println("Move 1 from left to right.");
            return;
        }
        leftToMid(n-1);
        System.out.println("Move "+n+" from left to right.");
        midToRight(n-1);
    }

    private static void midToRight(int n) {
    
    
        if (n==1) {
    
    
            System.out.println("Move 1 from mid to right.");
            return;
        }
        midToLeft(n-1);
        System.out.println("Move "+n+" from mid to right.");
        leftToRight(n-1);
    }

    private static void leftToMid(int n) {
    
    
        if (n==1) {
    
    
            System.out.println("Move 1 from left to mid.");
            return;
        }
        leftToRight(n-1);
        System.out.println("Move "+n+" from left to mid.");
        rightToMid(n-1);
    }
    private static void midToLeft(int n) {
    
    
        if (n==1) {
    
    
            System.out.println("Move 1 from mid to left.");
            return;
        }
        midToRight(n-1);
        System.out.println("Move "+n+" from mid to left.");
        rightToMid(n-1);
    }
    private static void rightToMid(int n) {
    
    
        if (n==1) {
    
    
            System.out.println("Move 1 from right to mid.");
            return;
        }
        rightToLeft(n-1);
        System.out.println("Move "+n+" from right to mid.");
        leftToMid(n-1);
    }

    private static void rightToLeft(int n) {
    
    
        if (n==1) {
    
    
            System.out.println("Move 1 from right to left.");
            return;
        }
        rightToMid(n-1);
        System.out.println("Move "+n+" from right to left.");
        midToLeft(n-1);
    }

It can be seen that although method 1 is very straightforward, the code is very cumbersome. We might as well make the code more abstract. Every time the process of vacating space can be abstracted from this column to another column, we can get our first Two kinds of recursively print the code of the whole process of moving the n-layer Tower of Hanoi from the leftmost to the rightmost.

Solution two

    public static void Hanoi2(int n) {
    
    
        if (n>0) {
    
    
            fun(n,"left","right","mid");
        }
    }
    public static void fun(int n,String from,String to,String other) {
    
    
        if (n==1) {
    
    
            System.out.println("Move 1 from "+from+" to "+to+"." );
        }else{
    
    
            fun(n-1,from,other,to);
            System.out.println("Move "+n+" from "+from+" to "+to+".");
            fun(n-1,other,to,from);
        }
    }

Guess you like

Origin blog.csdn.net/m0_53328239/article/details/130665422