Programmer's algorithm class (3) - recursion (recursion) algorithm

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/m0_37609579/article/details/99772243

First, what is recursive

Recursion is on a mathematical divide and autonomous thinking.

  1. Recursion large complex problems into the same problem as the original but smaller
  2. The deal with the problem
  3. Recursive need to have the boundary conditions, when the boundary conditions are not met, the recursive proceed; when the boundary conditions are met, recursive stop

[Baidu Encyclopedia] program that calls itself recursively called programming skills (recursion). As a kind of recursive algorithm in a programming language widely applications. A procedure or function directly or indirectly calls itself a way, it is usually a large, complex problem into a layer similar to the original problem to solve smaller problems in its definition or description, Recursion only requiring less program can describe the problem-solving process is repeated calculation needed, greatly reducing the code size.

Second, recursive mathematical representation

Third, the recursive solution of some mathematical problems

1. Fibonacci number

One of the classic mathematical problem; Fibonacci number, also known as golden columns, referring to such a series: 1,1,2,3,5,8,13,21, ......

This series of laws, that is, the first two and the value of the third term,

 
  1.  
    public class Solution {
  2.  
    public int Fibonacci(int n) {
  3.  
    if(n == 0)
  4.  
    return 0;
  5.  
    if(n == 1)
  6.  
    return 1;
  7.  
    return Fibonacci(n-2) + Fibonacci(n-1);
  8.  
    }
  9.  
    }
 

When n is large, you can obviously feel algorithm running more slowly, which is due to the return code uses two layers of recursion, recursive idea is good, but here we can use an iterative algorithm significantly improve operational efficiency, space for time.

 
  1.  
    public class Solution {
  2.  
    public int Fibonacci(int n) {
  3.  
    if(n < 2)
  4.  
    return n;
  5.  
    int f = 0, g = 1;
  6.  
    int result = 0;
  7.  
    for(int i = 1; i < n; i++){
  8.  
    result = f + g;
  9.  
    f = g;
  10.  
    g = result;
  11.  
    }
  12.  
    return result;
  13.  
    }
  14.  
    }
 

2. Tower of Hanoi

Tower of Hanoi is a puzzle game originated in India, also called the Towers of Hanoi. According to legend, it originated from three King column Hindu mythology Brahma created, a stack of the upper and lower small to large 64 gold discs on the column. Brahma command Brahman these discs in ascending order of movement to the other columns, wherein the large disk can not be placed above the small disk. When this 64 disc completely moved, the world will be destroyed.

æ ± 诺å¡æ¸¸æ

 
  1.  
    public class Hanoilmpl {
  2.  
     
  3.  
    public void hanoi(int n, char A, char B, char C) {
  4.  
    if (n == 1) {
  5.  
    move(A, C);
  6.  
    } else {
  7.  
    hanoi(n - 1, A, C, B);//步骤1 按ACB数序执行N-1的汉诺塔移动
  8.  
    move(A, C); //步骤2 执行最大盘子移动
  9.  
    hanoi(n - 1, B, A, C);//步骤3 按BAC数序执行N-1的汉诺塔移动
  10.  
    }
  11.  
    }
  12.  
     
  13.  
    private void move(char A, char C) {//执行最大盘子的从A-C的移动
  14.  
    System.out.println("move:" + A + "--->" + C);
  15.  
    }
  16.  
     
  17.  
    public static void main(String[] args) {
  18.  
    Hanoilmpl hanoi = new Hanoilmpl();
  19.  
    System.out.println("移动汉诺塔的步骤:");
  20.  
    hanoi.hanoi(3, 'a', 'b', 'c');
  21.  
    }
  22.  
    }
 

3. The eight queens problem

Eight queens problem is an old and well-known problems, is a typical case of backtracking algorithms. The problem is an international chess player Max Bethel presented in 1848: placed on the international chess 8 × 8 grid of eight Queens, it can not attack each other, that is, any two queens can not be in the same line, on the same row or the same slash and asked how many pendulum method. Gauss think there are 76 kinds of programs. 1854 in Berlin on different chess magazine published 40 kinds of different solutions, then someone solve 92 kinds of results using graph theory.

In an 8 ° ¡ 8 chessboard, eight queens, each accounting for a Queen grid; requirements do not appear to each other "attack" phenomenon between the Queen, that can not have two queens in the same row, the same column or the same diagonal .

Algorithm thinking

  1. Initialization: i = 1
  2. Initialization: j = 1
  3. Starting from the i-th row, j is the current value of the recovery, the j-th position is determined

         . A queen can be placed in position j: mark position (i, j), i ++, go to step 2

         . B can not be placed in position j Queen: j ++, a transfer step

         c. When j> 8, i--, go to step 3

     4. End: Line 8 queen can be placed in the position

Note: When using recursion, we need to map [row] [i] reset to 0 after execution, ensuring a checkerboard arrangement when empty

 
  1.  
    /**
  2.  
    * 递归算法之八皇后问题
  3.  
    *
  4.  
    * @author Administrator
  5.  
    */
  6.  
    public class Bahuanghou {
  7.  
    //定义一个8*8的矩阵
  8.  
    public static int[][] map = new int[8][8];
  9.  
    public static int count = 1;
  10.  
     
  11.  
    /**
  12.  
    * 显示棋盘方法
  13.  
    */
  14.  
    public static void show() {
  15.  
    System.out.println("第" + count + "中排列方式");
  16.  
    for (int i = 0; i < 8; i++) {
  17.  
    for (int j = 0; j < 8; j++) {
  18.  
    System.out.print(map[i][j] + " ");
  19.  
    }
  20.  
    System.out.println();
  21.  
    }
  22.  
    count++;
  23.  
    }
  24.  
     
  25.  
    /**
  26.  
    * 检验方法(验证该位置是否可以放皇后)
  27.  
    */
  28.  
    public static boolean check(int row, int col) {
  29.  
    //上面(行减小 列不变)
  30.  
    for (int i = row - 1; i >= 0; i--) {
  31.  
    if (map[i][col] == 1) {
  32.  
    return false;
  33.  
    }
  34.  
    }
  35.  
     
  36.  
    //左斜上 (行减小 列减小)
  37.  
    for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {
  38.  
     
  39.  
    if (map[i][j] == 1) {
  40.  
    return false;
  41.  
    }
  42.  
     
  43.  
    }
  44.  
    //右斜上 (行减小 列增加)
  45.  
    for (int i = row - 1, j = col + 1; i >= 0 && j < 8; i--, j++) {
  46.  
     
  47.  
    if (map[i][j] == 1) {
  48.  
    return false;
  49.  
    }
  50.  
     
  51.  
    }
  52.  
    return true;
  53.  
    }
  54.  
     
  55.  
    /**
  56.  
    * 八皇后算法
  57.  
    */
  58.  
    public static void play(int row) {
  59.  
     
  60.  
    //遍历当前行的所有单元格
  61.  
    for (int i = 0; i < 8; i++) {
  62.  
    //判断本格是否可以放皇后
  63.  
    if (check(row, i)) {
  64.  
    map[row][i] = 1;
  65.  
    //判断是否为最后一行
  66.  
    if (row == 7) {
  67.  
    show();
  68.  
    } else {
  69.  
    //接着走下一行
  70.  
    play(row + 1);
  71.  
    }
  72.  
     
  73.  
    //取消当前落子 清空棋盘
  74.  
    map[row][i] = 0;
  75.  
    }
  76.  
    }
  77.  
    }
  78.  
     
  79.  
    public static void main(String[] args) {
  80.  
    play(0);
  81.  
    }
  82.  
    }
 

IV Summary

For recursive thinking can be summarized as follows:

  • Recursive programming holds the best ideas "frog" is;
  • Recursion is characterized by: it once only going to solve a little problem.

For recursive implementation can follow two things:

  • Recursive export conditions;
  • Law obtained are summarized method.

My micro-channel public number: architecture Scriptures (id: gentoo666), shared Java dry, high concurrency programming, popular technical tutorials, and distributed micro-services technology, architecture, design, block chain technology, artificial intelligence, big data, Java interview questions, as well as cutting-edge information and so popular. Updated daily Oh!

References:

  1. https://blog.csdn.net/qq_41359051/article/details/85276387
  2. https://blog.csdn.net/qq_35256722/article/details/52728739
  3. https://blog.csdn.net/u010183728/article/details/81238401
  4. https://blog.csdn.net/xb2355404/article/details/79144451
  5. https://blog.csdn.net/m0_37618340/article/details/82635031

Guess you like

Origin www.cnblogs.com/anymk/p/11479439.html