Java - Recursive Classic Problem (Tower of Hanoi and Frog Jumping Stairs)

content

1. Tower of Hanoi problem

1. Problem overview

2. Solution analysis

3. Code implementation

2. Frog jumping steps

1. Problem overview

2. Solution analysis

3. Recursive implementation

4. Iterative implementation


1. Tower of Hanoi problem

1. Problem overview

2. Solution analysis

Summarize:

(1) Using disk C as an intermediary, move disks 1 to n-1 from lever A to lever B;

(2) Move the nth disc remaining in the A rod to the C rod;

(3) With lever A as an intermediary; move discs 1 to n-1 from lever B to lever C.   

(4) One plate needs to be moved 1 time, two plates need to be moved 3 times, three plates need to be moved 7 times, it is not difficult to come out, n plates need to be moved 2^n -1 times

3. Code implementation

public class TestDemo {
    //1个盘子时:A->C 1
    //2个盘子时:A->B A->C B->C 3
    //3个盘子时:A->C A->B C->B A->C B->A B->C A->C 7
    public static void move(char pos1,char pos2){
        //模拟鼠标操作
        System.out.print(pos1+"->"+pos2+" ");
    }
    /*
    n 代表盘子个数
    pos1 盘子的起始位置
    pos2 盘子的中转位置
    pos3 盘子的结束位置
     */
    public static void hanoi(int n,char pos1,char pos2,char pos3){
        if(n==1){
            move(pos1,pos3);
        }else{
            hanoi(n-1,pos1,pos3,pos2);//n-1次的结束位置应该是B上面,除了最下面的盘子,其他全被移到了B上
            move(pos1,pos3);//此时A上面仅剩下最下面的盘子,只需要将它移到C上
            hanoi(n-1,pos2,pos1,pos3);//此时盘子数是没有最下面的盘子,我们仅需要把B上的盘子通过A移动到C上
        }
    }

    public static void main(String[] args) {
        hanoi(1,'A','B','C');
        System.out.println();
        hanoi(2,'A','B','C');
        System.out.println();
        hanoi(3,'A','B','C');
        System.out.println();
        hanoi(4,'A','B','C');
        System.out.println();
        hanoi(5,'A','B','C');
    }
}

2. Frog jumping steps

1. Problem overview

A frog can jump up 1 steps at a time, or 2 steps at a time. Find how many ways the frog can jump up a n steps 

2. Solution analysis

3. Recursive implementation

public class TestDemo2 {
    public static int frogJump(int n){//台阶数量
        if(n==1 || n==2){
            return n;
        }else{
            return frogJump(n-1)+frogJump(n-2);
        }
    }

    public static void main(String[] args) {
        System.out.println(frogJump(1));
        System.out.println(frogJump(2));
        System.out.println(frogJump(3));
        System.out.println(frogJump(4));
        System.out.println(frogJump(5));
    }
}

4. Iterative implementation

public class TestDemo2 {
    public static int frogJump(int n){//台阶数量
        if(n==1 || n==2){
            return n;
        }
        int f1 = 1;
        int f2 = 2;
        int f3 = 0;
        for (int i = 3; i <=n ; i++) {
            f3 = f1+f2;
            f1 = f2;
            f2 = f3;
        }
        return f3;
    }

    public static void main(String[] args) {
        System.out.println(frogJump(1));
        System.out.println(frogJump(2));
        System.out.println(frogJump(3));
        System.out.println(frogJump(4));
        System.out.println(frogJump(5));
    }
}

Guess you like

Origin blog.csdn.net/weixin_55752048/article/details/120999065