Java: recursion and maze backtracking

Posted by Exoon on Wed, 19 Jan 2022 23:42:47 +0100

preface

Finally started the algorithm...
Recursion is really abstract. Without basic principles, it is like metaphysics...

#1, What problems can be solved by recursion! [Please add picture description]( https://img-blog.csdnimg.cn/ebcf0781fd71470ab1b18ce8bcf27127.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5pys6Imy5rip5Y2O,size_20,color_FFFFFF,t_70,g_se,x_16)

2, Recursive call mechanism

  • The calling mechanism is to return the result to whoever calls it, and then execute the code program from top to bottom

  • About this printing problem: assign text(4). The difference between adding else to the output and not adding else is: 1. If not, the last text(2) does not meet the recursion condition. text(2) returns to text (3) after executing the program (printing the current n value), that is, text(2) is executed in text (3), and then points to its own print statement, Then it is the same as text(4): the current n value will be printed every time you go back. Add else: the current n value is printed only when the recursion condition is not met in the last text(2), and the other text (3) and text(4) are met, so the else statement is not executed when returning: that is, add else to the print statement, and it will only print once when the recursion condition is not met. (print n=2)

  • About factorial: This is actually easy to understand. Go through it yourself and follow the top-down execution. A statement should be executed to the end (especially recursion). After he returns all the results, execute the following statements

2. Calling rules

3, Maze problem using recursion

1. Description

2. Code implementation

public class MiGong {
    public static void main(String[] args) {
        //Create a two-dimensional array to simulate the maze
        //Map
        int map[][] = new int[8][7];//Initialization of array
        //Use 1 for walls
        //First set the top and bottom to 1
        for (int i = 0; i < 7; i++) {
            map[0][i] = 1;
            map[7][i] = 1;
        }
        //Set the left and right to 1
        for (int i = 0; i < 8; i++) {
            map[i][0] = 1;
            map[i][6] = 1;
        }
        //Set baffle, 1 indicates
        map[3][1] = 1;
        map[3][2] = 1;
        //More clearly, there are three
        map[6][4] = 1;
        //Output map
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 7; j++) {
                System.out.print(map[i][j] + " ");
            }
            System.out.println();//Type a line and wrap it
        }
        //Start the test from [1] [1]
        if (setWay(map, 1, 1)) {
            System.out.println("Wayfinding:");
            //Output map after route finding
            for (int i = 0; i < 8; i++) {
                for (int j = 0; j < 7; j++) {
                    System.out.print(map[i][j] + " ");
                }
                System.out.println();//Type a line and wrap it
            }
        }
    }

    /**
     * @param map
     * @param i   Start position
     * @param j
     * @return If map[6][5] is found, it means it is found
     * Agreement: the strategy of finding the way is to go down "right" and "up" and "left". If this point fails, go back,
     * map[i][j]=0,It means you haven't walked, 1 means the fence, 2 means you can walk through, and 3 means you can walk through, which is a dead end
     */
    public static boolean setWay(int[][] map, int i, int j) {
        if (map[6][5] == 2)//Export
            return true;
        if (map[i][j] == 0) {//Judge the position passed in. If it is 0
            //Suppose this point can go through according to the strategy
            map[i][j] = 2;
            if (setWay(map, i + 1, j)) {//Go down and return true if you can reach the target
                return true;
            } else if (setWay(map, i, j + 1)) {//towards the right
                return true;
            } else if (setWay(map, i - 1, j)) {//Upward
                return true;
            } else if (setWay(map, i, j - 1)) {//towards the left
                return true;
            } else {//After the above series of walking methods, it shows that this point can not go, so it is marked as 3
                map[i][j] = 3;
                return false;
            }
        } else {//If this point is not 0, there are three cases 1, 2, 3
            //Why does this directly return an error? This is very spiritual. You can understand it by following the program
            return false;
        }
    }
}

summary

I know how to go back, but I don't understand it very much. After a point is marked 3, it directly returns false when it meets 3 again. How he meets 3 to turn is very spiritual and doesn't understand... Let's talk later...

Topics: Java Algorithm data structure