recursion (java)

1. Recursive application scenarios

Look at a practical application scenario, maze problem (backtracking), recursion (Recursion)

1678007343346

2. The concept of recursion

To put it simply: recursion means that the method calls itself, passing in different variables each time it is called . Recursion helps programmers solve complex problems and at the same time makes the code concise.

3. Recursive call mechanism

I cite two small cases to help you understand recursion. Some students have already learned recursion. Here I will review the recursive calling mechanism for you.

1) printing problem

2) Factorial problem

1678007819020

4. Code demonstration

package com.yt;

/**
 * @auther yt
 * @address https://www.cnblogs.com/y-tao/
 */
public class RecursionTest {
    public static void main(String[] args) {
        //通过打印问题,回顾递归调用机制
//        test(4);
        int res = factorical(4);
        System.out.println(res);
    }

    //打印问题
    public static void test(int n){
        if (n>2){
            test(n-1);
        } //else {
            System.out.println("n=" + n);
        //}
    }
    //阶乘问题
    public static int factorical(int n){
        if (n==1){
            return 1;
        } else {
            return n * factorical(n-1);
        }
    }
}

5. What kind of problems can recursion solve?

1) Various mathematical problems such as: 8 queens problem, Tower of Hanoi, factorial problem, maze problem, ball and basket problem (google programming contest)

2) Recursion is also used in various algorithms, such as quick sort, merge sort, binary search, divide and conquer algorithm, etc.

3) The problem that will be solved with the stack --> the recursive code is relatively concise

6. Important rules to follow for recursion

1) When a method is executed, a new protected independent space (stack space) is created

2) The local variables of the method are independent and will not affect each other

3) If the variable used in the method is a reference type, each stack will share the data space.

4) The recursion must approach the condition of exiting the recursion , otherwise it will be infinite recursion, dead turtle :)

5) When a method is executed or returns, it will return, and whoever calls it will return the result to whomever it is called. At the same time, when the method is executed or returned, the method will be executed.

7. Recursive implementation of the maze problem

package com.yt;

/**
 * @auther yt
 * @address https://www.cnblogs.com/y-tao/
 */
public class MiGong {
    public static void main(String[] args) {
        //先创建一个二维数组,模拟迷宫
        //地图
        int[][] map = new int[8][7];
        //1 表示墙
        //上下全部设置为1
        for (int i = 0; i < 7; i++) {
            map[0][i] = 1;
            map[7][i] = 1;
        }

        //左右全部设置为1
        for (int i = 0; i < 8; i++) {
            map[i][0] = 1;
            map[i][6] = 1;
        }

        //设置挡板,1表示墙
        map[3][1] = 1;
        map[3][2] = 1;
        //输出地图
        System.out.println("地图的情况");
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 7; j++) {
                System.out.print(map[i][j] + " ");
            }
            System.out.println();
        }

        setWay(map,1,1);

        //输出新的地图,小球走过,并标记过的递归
        System.out.println("小球走过,并标记过的地图的情况");
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 7; j++) {
                System.out.print(map[i][j] + " ");
            }
            System.out.println();
        }

    }

    //使用递归回溯来给小球找路
    //说明
    //1.map表示地图
    //2. i,j表示从地图的哪个位置开始出发(1,1)
    //3.如果小球能到map[6][5]位置,则说明通路找到
    //4.约定:当map[i][j]=0 表示该点没有走过,当为1时表示墙,2表示通路可以走;3表示改点已经走过,但是走不通
    //5.在走迷宫时,需要确定一个策略(方法)下->右->上->左,在回溯

    /**
     *
     * @param map 表示地图
     * @param i 从哪个位置开始找
     * @param j
     * @return 如果找到通路,就返回true,否则返回false
     */
    public static boolean setWay(int[][] map, int i, int j){
        if (map[i][j]==2){
            //通路已经找到
            return true;
        } else {
            if (map[i][j]==0){//如果当前这个点还没有走过
                //按照策略 下 右 上 左 走
                map[i][j] = 2;//假定该点是可以走通的
                if (setWay(map,i+1,j)){//向下走
                    return true;
                } else if (setWay(map,i,j+1)){
                    //向右走
                    return true;
                } else if (setWay(map,i-1,j)){
                    //向上走
                    return true;
                } else if (setWay(map,i,j-1)){
                    //向左走
                    return true;
                } else {
                    //说明该点是走不通的,是死路
                    map[i][j] = 3;
                    return false;
                }

            } else {
                //说明map[i][j]!=0,可能是1,2,3
                return false;
            }

        }

    }
}

Guess you like

Origin blog.csdn.net/m0_57385165/article/details/129374189