Top ten algorithms commonly used by programmers (2): divide and conquer algorithm to solve the Tower of Hanoi problem

Divide and conquer algorithm

Introduction: Divide and conquer, divide a complex problem into two or more identical or similar problems, and then divide the sub-problems into smaller sub-problems... until the final sub-problem can be solved simply, the solution of the original problem is the sub-problem Combination of solutions . This technique is the basis of many efficient algorithms. It
can be solved:
binary search,
checkerboard coverage,
merge sort,
quick sort,
Tower of Hanoi,
etc...

Basic steps: The
divide-and-conquer method has three steps in each layer 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 scaled If it is small and easy to solve, it is solved directly, otherwise the sub-problems are solved recursively.
3) Merging: Combine the solutions of each sub-problem into the solution of the original problem.

Case:

The divide and conquer algorithm solves the problem of the Tower of Hanoi: the
problem of the Tower of Hanoi: the problem of the
Tower of Hanoi (also known as the Tower of Hanoi) is an educational toy derived from an ancient legend in India. 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 Great Brahma ordered the Brahmin to re-place the disc on another pillar in order of size from below. It is also stipulated that the disc cannot be enlarged on the small disc, and only one disc can be moved between the three pillars at a time.
Go and experience this game: Tower of Hanoi

One disk:

directly move the disk from A to C in one step.
Two disks:
Insert picture description here
first move the small disk to Tower B, then move the large disk to Tower C, and then put the small disk on the large disk.
Three discs:
Insert picture description here
the upper two discs can be regarded as a whole, move the upper two discs to tower B according to the second step, move the bottom disc to tower c, and then move the two according to the second step The disk moves to the C tower.

Thinking analysis:
1) If there is only one disk, A->C
2) If there is n>=2, we can always consider two disks: 1. The bottom disk 2. The top disk
2.1) Put first The top disk A->B
2.2) The favorite disk A->C
2.3) All disks in Tower B from B->C

Code demo:


public class DAC {
    
    
	public static void main(String[] args) {
    
    
		hanoiTower(3,'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) {
    
    
		//如果只有一个盘
		if(num==1) {
    
    
			System.out.println("第一个盘从"+a+"->"+c);
		}else {
    
    //num>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);
		}
	}
}

Take the three disks as an example, the steps are:
Insert picture description here
7 steps in total

Guess you like

Origin blog.csdn.net/qq_45273552/article/details/109286214