Java learning -- recursion and maze of data structures

recursion

Recursion is to call itself. Different variables are passed in each call, which helps programmers solve complex problems.

1. What kind of problem is recursion used to solve

  • Various mathematical problems, such as eight queens problem, Hanoi Tower, factorial problem, maze problem, ball and basket problem

  • Recursion will also be used in various algorithms, such as fast sorting, merge sorting, binary search, divide and conquer algorithm and so on.

     

2. Principles of recursion

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

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

  • If a variable of reference type (such as array) is used in the method, the data of the reference type will be shared.

  • Recursion must approach the condition of exiting recursion, otherwise it is infinite recursion and StackOverflowError error occurs

  • When a method is executed or encounters a return, it will return. The result will be returned to whoever calls it. At the same time, when the method is executed or returned, the method will also be executed.

     

3. Maze problem

3.1 ideas
  • Create a maze map using a two-dimensional array, containing only 0 and 1. Where: 0 represents the point that has not been passed, and 1 represents the wall

  • Assuming that the ball needs to go from point A to point B, set the initial position point A as the path, and set its value as 2

  • Continuously judge whether the point in the upper, lower, left and right directions of point A is A path through recursion. If it is A path, set the value of the point as 2, and then judge whether the upper, lower, left and right directions of the path are paths through recursion until point B; If none of the points in the four directions above and below is A path, set the point as A dead end, set it as 3, trace back to the previous point, and then re judge whether the previous point is A path until point B.

  • If the points with 0 in the four directions up, down, left and right of point A (points on the road that have not been passed) have been passed once and are set to 3 (dead road), it means that there is no road from point A to point B, and the maze cannot go out, and the result is false

3.2 code implementation:

(Note: if the reader does not know the specific process of the program, he can use the breakpoint method to check the values of i and j step by step, so as to understand the walking route of the ball)

public class Maze {
    public static void main(String[] args) {
        //First create a two-dimensional array to simulate the maze
        //Map
        int[][] map = new int[8][7];
        //Use 1 for walls
        //Set up and down to 1
        for(int i=0;i<7;i++){
            map[0][i]=1;
            map[7][i]=1;
        }
        //All left and right are set to 1
        for(int i=0;i<8;i++){
            map[i][0]=1;
            map[i][6]=1;
        }
        //Set baffle 1 to indicate
        map[3][1]=1;
        map[3][2]=1;
        map[2][2]=1;

        System.out.println("The original map is as follows:");
        for (int i=0;i<8;i++) {
            for (int j = 0; j < 7; j++) {
                System.out.printf(map[i][j] + "\t");
            }
            System.out.println();
        }

        //Use recursive backtracking
        boolean result = setway(map,1,1);
        //Export new map
        System.out.println("=========================");
        for (int i=0;i<8;i++) {
            for (int j = 0; j < 7; j++) {
                System.out.printf(map[i][j] + "\t");
            }
            System.out.println();
        }

    }
    //Find the way through recursive backtracking
    //1.map Represent map
    //2.i,j Indicates where to start from on the map
    //3.If the ball can reach map[6][5]Location, indicating the way
    //4.Agreement: when map[i][j]A value of 0 indicates that the point has not passed, 2 indicates that the path can be taken, and 3 indicates that the path has passed but can not be taken
    //5.When walking through the maze, you need to determine a strategy: next->right->upper->Left,If this point doesn't work, go back(Note that the steps of this policy can change the order)
    //The so-called up, down, left and right directions actually determines whether the points in the up, down, left and right directions of each point are paths through a recursive algorithm. If it is a dead road, it is marked as 3, indicating that it is impossible to go and backtracking is carried out
    public static boolean setway(int[][]map,int i,int j){
        if(map[6][5]==2){//The path has been found
            return true;
        }else {
            if(map[i][j]==0){//If the current point has not passed
                //According to the strategy->right->upper->Left walk
                
                map[i][j] = 2;  //It is assumed that this point can go through
                if(setway(map,i+1,j)){ //Go down
                    return true;
                }else if(setway(map,i,j+1)){//turn right
                    return true;
                }else if(setway(map,i-1,j)){//Go up
                    return true;
                }else if(setway(map,i,j-1)){//turn left
                    return true;
                }else {
                    //It means that the four directions of this point are impassable and encounter a dead end
                    map[i][j]=3;
                    return false;
                }
            }else {
                return false;
            }
        }
    }
}

 

Posted on Wed, 01 Dec 2021 22:04:54 -0500 by jcoones