Case of a recursive data structure

What is recursion?

As the name suggests, is a so-called recursive function (or method) calls itself, the simplest of the following:

public void text() {
        text();
}
  • It is that simple, but be sure to give this a recursive function exit, otherwise it will infinite loop, the end result is OutOfMemory (out of memory), if it is called in the main function, it would stack space is full of errors.
  • How to write a recursive method export it?
    As long as the recursive process, there is a way to have a return statement, there is no longer a recursive recursive method will quit.
    We add a condition to return to the method;
private int count = 0;
/*
*   每次让count累加,当它等于4时,就返回,这样就能这个递归函数了
*/
public void text() {
   if (count == 4) {
      return;
   }
      count++;
      text();
}

Recursive what's the use?

1. The most famous Pei Fibonacci series

Has the following problem: Suppose the first month with a pair of newborn rabbits, rabbit enters second month maturity, three months after the birth I bunnies, and one pair of rabbits will mature monthly birth of a small rabbit, rabbits never die. . . How many rabbits have n months
can easily be used to calculate the number of rabbits exhaustive months of the beginning

month 1 2 3 4 5 6 7
Rabbit logarithmic 1 1 2 3 5 8 13
  • Use recursion to solve the problem of thinking analysis:

It is easy to get a formula:
When n = 0,, 0 rabbits;
when n = 1, rabbits 1;
when n = 2, 1 rabbit, because it is the first three months to start having bunny
when n> 2, F. (n-) = F. (. 1-n-) F. + (2-n-)

When With the above formula, we would be very easy to write the following code:

public static int brithNew(int n) {
        if (n < 0) {
            return -1;
        }
        if (n == 0) {
            return 0;
        }
    //这个其实就是递归退出的条件,因为n一直的在递减
        if (n <= 2) {
            return 1;
        }

        return brithNew(n-1)+brithNew(n-2);
}

2. Using a recursive solve the maze

First using a two-dimensional array definition maze, the following rules:

  • First use 0 to represent all positions of this maze
  • Use 1 to indicate the outermost wall
  • Use 2 to represent this point has come, and is through
  • Use 3 to indicate this point has been passed, but nowhere
    maze defined below:
    Write a recursive labyrinth
    The red parts indicate wall, from the top left to bottom-right, first using a two-dimensional array definition maze.
private static void init(int[][] maze) {
        //定义所有的墙
        for (int i = 0; i < 10; i++) {
            maze[0][i] = 1;
            maze[7][i] = 1;
        }
        for (int i = 0; i < 8; i++) {
            maze[i][0] = 1;
            maze[i][9] = 1;
        }

        maze[3][1] = 1;
        maze[3][2] = 1;
        maze[3][3] = 1;
        maze[5][3] = 1;
        maze[5][4] = 1;

        maze[5][5] = 1;
        maze[4][5] = 1;
        maze[3][5] = 1;
        maze[2][5] = 1;
        maze[1][5] = 1;

        maze[2][7] = 1;
        maze[3][7] = 1;
        maze[4][7] = 1;
        maze[5][7] = 1;
        maze[6][7] = 1;
    }

Print this array, and on top of the picture, as follows:
Create a maze using a data structure

Then start looking for the path, particularly the following rules

  • Each point has a lower, right, and left four paths Alternatively, this order can be changed;
  • Replace the four sequence, a different result, it is possible to try to replace these four order to seek the shortest path is now
  • When we arrived at this point, we assume this point the path is correct, it is assigned to give 2
  • Each small recursive exit condition is the end value of 2, i.e. maze [9] [7] == 2
  • If the road is blocked, it will be current at this point is set to 3
private static boolean searchRoute(int maze[][], int x, int y) {
        int i = maze.length;
        int j = maze[0].length;

        if (maze[i - 2][j - 2] == 2) {
            //目标点的值为2,就说明这条路已经走完了
            return true;
        } else {
            if (maze[x][y] == 0) {//为0说明这这条可走,而且没有走过
                maze[x][y] = 2;
                //按照下、右、上、左的顺序
                if (searchRoute(maze, x + 1, y)) {//向下
                    return true;
                } else if (searchRoute(maze, x, y+1)) {//向右
                    return true;
                } else if (searchRoute(maze, x - 1, y)) {//向上
                    return true;
                } else if (searchRoute(maze, x, y - 1)) {//向左
                    return true;
                } else {
                    maze[x][y] = 3;
                    return false;
                }
            } else {
                return false;
            }
        }
    }

Create a maze using a data structure

Code is printed on top of the array used to:

public static void printlnArr(int arr[][]) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                System.out.print(arr[i][j] + "\t");
            }
            System.out.println();
            System.out.println();
        }
    }
Number of public attention, learning Java does not get lost

No public attention data structure

Guess you like

Origin www.cnblogs.com/Lyn4ever/p/12169283.html