Hanoi Tower problem of data structure and algorithm (Java recursion)

Keywords: Java

Hanoi Tower question:

There are three columns, source pole a, temporary storage pole temp. There are n-layer plates on target pole C A, which are arranged downward from small to large. Now it is necessary to move the plates of a pole to C pole

       Requirements: 1) the large disk is below and the small disk is above
             2) only one plate can be moved at a time
             
         Personal thinking: analyze the problem first and use mathematical induction
                 When there is only one disk, move directly;
                 When there are two disks, first move the small disk to the temporary storage rod, then move the large disk to the destination rod C, and finally move the small disk of the temporary storage rod temp to the destination rod C;
                 When there are three disks, write the detailed steps in the comments in the following code
                 ......
                 When there are n disks, it is considered as two parts: the (n-1) disk on the top and the (n-1) disk on the top. First, move the (n-1) disk on the top from the A lever to the temp lever with the help of the C lever, then the N disk on the top is placed directly to the C lever, and the last recursive call is enough to solve the problem.
                 
                 The number of floors of Hanoi tower can be stored and read by program memory or input by keyboard. c is program counter to calculate the times of moving disk


import java.util.Scanner;

public class Hanoi {
    int c=0;//Counter, counting the number of moves
    public void moveone(int n, String A, String C) {
        System.out.println("move  " + n + "  from  " + A + "     to  " + C);
    }

    public void movesome(int n, String A, String temp, String C) {

        c++;
    
        if (n <= 0) {
            System.out.println("number error");
            return;
        } else if (n == 1) {
            moveone(n, A, C);
        } else {
            movesome(n - 1, A, C, temp);
            // First, move the (n-1) plates above from the A-bar to the temp bar with the help of the C-BAR
            moveone(n, A, C);
            // Then move the plate number n from the A-bar to the C-BAR
            movesome(n - 1, temp, A, C);
//            Move the remaining (n-1) plates from the temp lever to the C lever with the A lever
        }
        //Realization of three layers of handwritten Hanoi Tower on draft paper
//        moveone(1,A,C);
//        moveone(2,A,temp);
//        moveone(1,C,temp);
//        moveone(3, A, C);
//        moveone(1, temp, A);
//        moveone(2, temp, C);
//        moveone(1, A, C);

    }

    public static void main(String[] args) {
       //Layer number
        System.out.println("Please enter the number of floors of Hanoi Tower:");
        Scanner input =new Scanner(System.in);
                int  in=input.nextInt();
                int l = 3;

        Hanoi h = new Hanoi();
        //    h.moveone(l,A,C); / / program memory storage and reading
        h.movesome(in, "initial position", "Temporary storage place", "Destination address");
        System.out.println("The number of moves needed to realize the Hanoi Tower:"+h.c);

//        H. movehome (L, "initial location", "temporary storage location", "destination address");

    }
}

Program running results:

Posted by romic on Sat, 30 Nov 2019 06:59:49 -0800