The classic case of Java divide and conquer algorithm: Tower of Hanoi

divide and conquer algorithm

Thought

When we solve some problems, because these problems have to deal with a lot of data, or the solution process is quite complicated, the direct solution method takes a long time, or cannot be directly solved at all. For this type of problem, we often decompose it into several sub-problems first, find the solutions to these sub-problems, and then find a suitable method to combine them into a solution to the entire problem. If these sub-problems are too large to solve, they can be divided into several smaller sub-problems, and so on, until the solution can be found directly. This is the basic idea of ​​the divide and conquer strategy.

Divide and conquer problem solving steps

(1) Decomposition, divide the problem to be solved into several smaller-scale similar problems;
(2) Solve, when the sub-problems are divided into small enough, use a simpler method to solve;
(3) Merge, according to the requirements of the original problem, The solutions to the subproblems are combined layer by layer to form the solution to the original problem.

Classic divide and conquer algorithm - Tower of Hanoi

Tower of Hanoi: The Tower of Hanoi (also known as the Tower of Hanoi) is an educational toy that originated from an ancient legend in India. When Brahma created the world, he made three diamond pillars. On one pillar, 64 gold discs were stacked in order of size from bottom to top. Brahma ordered Brahmin to rearrange the discs on another pillar in order of size from below. And it is stipulated that the disk cannot be enlarged on the small disk, and only one disk can be moved between the three pillars at a time.

Java implementation of Tower of Hanoi
package dac;

public class HanoiTower {
    
    
    public static void main(String[] args) {
    
    
        //分治算法的汉诺塔问题
        hanoiTower(5,'A','B','C');
    }

    /**
     * @param num 一共有多少个盘
     * @param a   a塔
     * @param b   b塔
     * @param c   c塔
     */
    public static void hanoiTower(int num, char a, char b, char c) {
    
    
        //如果只有一个盘,从a移动到c
        if (num == 1) {
    
    
            System.out.println("第1个盘从 " + a + "——>" + c);
        } else {
    
    
            //如果有n>=2个盘,应该看成只有两个盘,将最下面的一个盘和上面的所有盘分开看待。
            //先把最上面的所有盘a——>b,移动过程会使用到c
            hanoiTower(num - 1, a, c, b);
            //把最下面的盘从a——>c
            System.out.println("第" + num + "个盘从 " + a + "——>" + c);
            //把B塔的所有盘从b——>c,移动过程会使用到a塔
            hanoiTower(num-1, b, a, c);
        }
    }
}
operation result
第1个盘从 A——>C
第2个盘从 A——>B
第1个盘从 C——>B
第3个盘从 A——>C
第1个盘从 B——>A
第2个盘从 B——>C
第1个盘从 A——>C
第4个盘从 A——>B
第1个盘从 C——>B
第2个盘从 C——>A
第1个盘从 B——>A
第3个盘从 C——>B
第1个盘从 A——>C
第2个盘从 A——>B
第1个盘从 C——>B
第5个盘从 A——>C
第1个盘从 B——>A
第2个盘从 B——>C
第1个盘从 A——>C
第3个盘从 B——>A
第1个盘从 C——>B
第2个盘从 C——>A
第1个盘从 B——>A
第4个盘从 B——>C
第1个盘从 A——>C
第2个盘从 A——>B
第1个盘从 C——>B
第3个盘从 A——>C
第1个盘从 B——>A
第2个盘从 B——>C
第1个盘从 A——>C

Process finished with exit code 0

Guess you like

Origin blog.csdn.net/weixin_42643321/article/details/107961309