"Little Monk's Game"--Tower of Hanoi

According to legend, there was a temple on the mountain, and there was a young monk and an old monk in the temple. One day the little apprentice was free, so he went to the old monk and asked if there were any games to exercise his intelligence. The old monk prepared three pillars for the little monk and said: First, there is only one bowl on the first pillar. The bowl was moved to the third pillar, and the young monk easily completed it; then the old monk put three bowls on the first pillar, and the three bowls were placed in order from small to large. This time he The little monk was asked to move these three bowls from the first pillar to the third pillar, but the small bowl must be on top of the large bowl during the moving process, and only one bowl could be moved at a time. The little monk thought for a while , Completed the goal; but in the end the old monk said, what if I give you 64 bowls? The little monk fell into thinking. Do you know how to do it smartly?

Tower of Hanoi (Hanoi Tower), also known as the Tower of Hanoi, derived from an ancient Indian legend. When the Brahma created the world, he made three diamond pillars. On one pillar, 64 golden discs were stacked in order of size from bottom to top. The Brahma ordered the Brahmin to re-place the disc on another pillar in order of size from below. It also stipulates that at any time, the disk cannot be enlarged on the small disk, and only one disk can be moved between the three pillars at a time. What should I do?

1. Move a plate : (1 step)

Insert picture description here
The kids know it, of course, just let it go. A--> C

Insert picture description here

2. Move two plates : (3 steps)

Insert picture description here
A1- - > B , A2 - - > C :

Insert picture description here
A1--> C:
Insert picture description here
3. Move three plates : (7 steps)
Insert picture description here
A1--> C, A2--> B:
Insert picture description here
A1--> B, A3--> C:
Insert picture description here
A1--> A, A2-- > C, A1--> C
Insert picture description here
We summarize the above rules :

Move one plate: 1 step = 2^1-1
Move two plates: 3 steps = 2^
2-1 Move three plates: 7 steps = 2^ 3-1

move 64 plates: 2^64-1

Let’s do a simple calculation. The computer runs 64 plates for nearly 300 years.
So we simply output the results of 1, 2, and 3 plates.

package test24;

public class testDemo {
    
    
    public static void move (char pos1,char pos2) {
    
    
        System.out.print(pos1+" --> "+pos2+" ");//模仿鼠标的移动
    }
    /**
     *
     * @param n       代表盘子的数目
     * @param pos1    代表第一根柱子  A
     * @param pos2    代表第二根柱子  B
     * @param pos3    代表第三根柱子  C
     */
    public static void HanoiTower (int n,char pos1,char pos2,char pos3) {
    
      //n代表盘子的数目
        if(n == 1) {
    
    
            move(pos1,pos3);
        } else {
    
    
            HanoiTower(n-1,pos1,pos3,pos2);   // pos1 上的东西通过 pos3 传到 pos2 上
            move(pos1,pos3);
            HanoiTower(n-1,pos2,pos1,pos3);
        }
    }
    public static void main(String[] args) {
    
    
        HanoiTower(1,'A','B','C');
        System.out.println();
        HanoiTower(2,'A','B','C');
        System.out.println();
        HanoiTower(3,'A','B','C');
        System.out.println();
    }

}

operation result:
Insert picture description here

If you want to try 64 plates, you can try it yourself! ! !

Guess you like

Origin blog.csdn.net/qq_45658339/article/details/108876299