Detailed Explanation: Divide and Conquer Algorithm [Java Implementation] - Hanoi Tower Problem

Divide and conquer algorithm:

1. Introduction of divide and conquer algorithm

Second, the basic steps of the divide and conquer algorithm:

Best Practices of Divide and Conquer Algorithm - Tower of Hanoi:

The Legend of the Tower of Hanoi:​

Demonstration and thinking analysis of the Tower of Hanoi game:

Java code implementation of the Tower of Hanoi game:


1. Introduction of divide and conquer algorithm

        Divide and conquer is a very important algorithm. The literal explanation is "divide and conquer", which is to divide a complex problem into two or more identical or similar sub-problems, and then divide the sub-problems into smaller sub-problems until the final sub-problems can be solved simply and directly. The solution of the problem is the combination of the solutions of the subproblems. This technique is the basis of many efficient algorithms, such as sorting algorithms (quick sort, merge sort), Fourier transform (fast Fourier transform)...

Second, the basic steps of the divide and conquer algorithm:

Divide and conquer has three steps at each level of recursion:

1) Decomposition : Decompose the original problem into several smaller, independent sub-problems with the same form as the original problem

2) Solve : If the sub-problem is small and easy to be solved, then solve it directly, otherwise solve each sub-problem recursively

3) Merge : Merge the solutions of each sub-problem into the solution of the original problem.

Best Practices of Divide and Conquer Algorithm - Tower of Hanoi:

The Legend of the Tower of Hanoi:

According to legend, in ancient Indian holy temples, there was a game called Hanoi Tower (Hanoi). The game is played on a copper plate device with three rods (numbered A, B, C). On rod A, 64 gold discs are placed in order from bottom to top and from big to small (as shown in the picture above). The goal of the game: move all the gold discs on pole A to pole C, and stack them in 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 placed on any rod A, B, or C during the operation.

Demonstration and thinking analysis of the Tower of Hanoi game:

1. If there is only one disk, move it: AC

Second, if we have n>=2, we can always see it as two disks: one is the bottom disk (one), and the other is the upper disk (multiple)

1) Move the top disk first: A->B

2) Move the bottom disk: A->C

3) Move all disks in Tower B: B->C

Java code implementation of the Tower of Hanoi game:

public class Hanoitower {//汉诺塔

    public static void main(String[] args) {
        //测试
//        hanoiTower(1,'A','B','C');//1个盘
//        hanoiTower(2,'A','B','C');//2个盘
//        hanoiTower(3,'A','B','C');//3个盘
        hanoiTower(5, 'A', 'B', 'C');//5个盘

    }

    /**
     * 汉诺塔移动的方法
     * 使用分治算法:
     * @param num 光盘的个数
     * @param a
     * @param b
     * @param c
     */
    public static void hanoiTower(int num, char a, char b, char c) {
        if (num == 1) {
            System.out.println("第1个盘:从" + a + "-->>" + c);
        } else if (num >= 2) {
            //如果我们有n>=2情况,我们总是可以看做是两个盘:1最下边的盘(一个)2,上面的盘(多个)
            //1 先把最上面的盘a-->>b,移动过程会使用到c
            hanoiTower(num - 1, a, c, b);//递归
            //2 把最下面的盘a-->>c
            System.out.println("第" + num + "个盘:从" + a + "-->>" + c);
            //3 把b塔的所有盘从b-->>c,移动过程会使用到a塔
            hanoiTower(num - 1, b, a, c);//递归
        }
    }
}

There will be times when the wind and the waves cleave, hang the clouds and sail straight to the sea!

Guess you like

Origin blog.csdn.net/m0_52729352/article/details/121934468