Divide and conquer algorithm _ Tower of Hanoi problem _ Java implementation

Original text: https://blog.csdn.net/ljmingcom304/article/details/50296939


Please indicate the source for reprinting: http://blog.csdn.net/ljmingcom304/article/details/50296939 
This article comes from: [Liang Jingming's blog]

1. Divide and conquer algorithm

  What is the divide and conquer algorithm? It is to divide a big problem that is difficult to solve into some smaller and relatively independent identical problems so that they can be broken down and divided and conquered.

2. Tower of Hanoi problem  

  The Tower of Hanoi problem is a very classic problem that can be solved by the divide and conquer algorithm. There are three stone pillars A, B, and C of the same size. Among them, the stone pillar A has n plates placed in order from bottom to top. Now we need to move all the plates of stone pillar A to stone pillar C, and only one disk can be moved at a time. The small disk cannot be placed on the large disk. How to move it? 

Tower of Hanoi
  

3. Algorithm analysis  

  When n=1, that is, only one disk is placed on the stone pillar A at the beginning, then the disk can be directly moved from the stone pillar A to the stone pillar B. 
  When n=2, the discs are numbered as No. 1 and No. 2 in order of size from top to bottom, then to move all the discs from Pillar A to Pillar C, firstly, the No. 1 disk needs to be moved to Pillar B, Then move the No. 2 disk to the stone pillar C, and finally move the No. 1 disk to the stone pillar C. 
  When n=3, the disks are still numbered as No. 1, No. 2 and No. 3 in the order of size from top to bottom. At this time, because the problem is relatively complicated, the No. 1 and No. 2 disks are regarded as one disk, that is For discs 1+2, what needs to be solved at this time is the problem of moving discs 1+2 and 3 to stone pillar C, that is, first move disks 1+2 to stone pillar B, and then move No. 3 disks to stone pillar B. Move the disk to Pillar C, and finally move the No. 1+2 disk to Pillar C. 
  Since only one disc can be moved at a time, if you want to move the No. 1+2 disc to the stone pillar B, you need to split the No. 1+2 disc into two individuals, which is regarded as the No. 1 and No. 2 discs. Move to Pillar B, and move the No. 1+2 disk to Pillar C in the same way. 
  And so on... 
  When n=n, the discs are numbered from top to bottom as No. 1, No. 2, No. 3...n No., and in the same way, the No. 1 to No. n-1 discs are regarded as one disc , namely  n1(n1)∑1n(n−1) disk, the solution at this time is to convert n1(n1)The problem of moving the ∑1n(n−1) disc and the n-numbered disc to the stone pillar C, that is, first moven1(n1)∑1n(n−1) disk is moved to pillar B, then n disk is moved to pillar C, and finallyn1(n1)The disk ∑1n(n−1) moves to the stone pillar C. Because after moving the nth disk to the stone pillar C, no matter how the first n-1 disks move, there is no need to move the nth disk again, that is, the parent problem and the child problem are relatively independent and do not affect each other, so we can willn1(n1)Similarly, the problem of ∑1n(n−1) disks can be split down and moved downwards. 
  Sample code:

public class FZSFProblem {

    public static void main(String[] args) {
        solve(3);
    }

    public static void solve(int n) {
        // Known conditions n disks and three pillars A, B, C
        hanoi(n, "A", "B", "C");
    }

    /**
     * To make the nth disk move from A to C successfully, you need to make the first n-1 disks move from A to B first, and then let the nth disk move from A to C,
     * Finally, let the n-1th disk move from B to C. As for how to move the first n-1 disks from A to B or from A to C, just ask the parent
     * For sub-questions with the same question, the solution of the parent question can be used.
     */
    private static void hanoi(int n, String a, String b, String c) {
        if (n == 1) {
            // When there is only one disc, move directly from the A pillar to the C pillar
            move(n, a, c);
        } else {
            // Move the first n-1 discs from Pillar A to Pillar B
            hanoi (n - 1, a, c, b);
            // Move the nth disk from Pillar A to Pillar C
            move(n, a, c);
            // Move the first n-1 discs from Pillar B to Pillar C
            hanoi (n - 1, b, a, c);
        }
    }

    private static void move(int n, String i, String j) {
        System.out.println("The "+n+"th disc,"+"moves from"+i+"to"+j);
    }

}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324681024&siteId=291194637