Tear recursion by hand (Explanation of C code examples of the Tower of Hanoi problem)

recursion

Pave the way for the problem

A simple recursive program

The following program can achieve the sum of integers from 1 to 100 by recursion

# include <stdio.h>

//递归求和
int f(int n){
    
    
    int res;
    if(n == 1){
    
    
        return 1;
    }else{
    
    
        return n + f(n - 1);
    }
}

int main(){
    
    
    int a = f(100);
    printf("%d", a);
    return 0;
}

Recursive internal mechanism

When a function is called during the execution of another function, the system needs to do three things before running the called function:

  • Pass all the actual parameters , return address and other information to the called function to save
  • Allocate storage space for local variables (including formal parameters) of the called function
  • Transfer control to the entry point of the called function

Before returning from the called function to the calling function, the system must complete three things:

  • Save the return result of the called function
  • Release the storage space allocated by the called function
  • Transfer control to the calling function according to the return address saved by the called function

When multiple functions call each other, according to the principle of "return after call", the information transfer and control transfer between the above functions must be realized with the help of "stack", that is, the system arranges the data space required by the entire program when it is running. In a stack, whenever a function is called, a storage area is allocated on the top of the stack, and the stack operation is performed. Whenever a function exits, its storage area is released, and the stack operation is performed. The currently running function is always at the top of the stack

Recursion satisfies three conditions

  • Recursion must have an explicit termination condition

  • The size of the (remaining) data processed by this function must be decreasing

  • The transformation must be solvable

Loops and recursion?

recursion:

  • Ease of understanding (simple idea, solves a specific problem)
  • slow
  • large storage space

cycle:

  • difficult to understand
  • high speed
  • small storage space

Towers of Hanoi Problem Implementation

insert image description here

Ideas:

If it is a plate,

Directly move the disk on state A to state C

otherwise,

1. First move the n-1 disks in state A to state B with the aid of state C

2. Directly move the disk on state A from state A to state C

3. Finally, move the n-1 disks in state B to state C with the aid of state A

The ABC state is used here to illustrate the problem instead of the ABC column because a certain column will not be fixed as the source, middle, and target during the movement, but will continue to change dynamically. However, we understand it as three states: source , middle, target.

# include <stdio.h>

/*
如果是一个盘子,
    直接将A状态上的盘子直接移动至C状态
否则,
    1.先将A状态上的n-1个盘子借助C状态移动到B状态
    2.直接将A状态上的盘子从A状态移动到C状态
    3.最后将B状态上的n-1个盘子借助A状态移动到C状态

*/
int count = 0;
//柱子编号是逻辑上的,每次调用会变化,A为源,B为中间过程,C为目标
int Hanoi_Tower(int n, char A, char B, char C){
    
      
    if(n == 1){
    
    
        count++;   
        printf("%d.将编号为%d的盘子直接从%c柱子移动到%c柱子\n", count, n, A, C);
    }
    else{
    
    
        //1.将n-1个从A借助C移动到B
        Hanoi_Tower(n - 1, A, C, B);
        //2.直接将A剩下的一个移入目标柱子
        count++;
        printf("%d.将编号为%d的盘子直接从%c柱子移动到%c柱子\n", count, n, A, C);
        //3.将剩下的n-1个盘子从B借助A移动到C
        Hanoi_Tower(n - 1, B, A, C);
    }
}


int main(){
    
    
    char ch1 = 'A';
    char ch2 = 'B';
    char ch3 = 'C';
    int n;
    printf("请输入要移动的盘子个数:\n");
    scanf("%d", &n);
    printf("要移动的盘子个数为:%d\n", n);
    Hanoi_Tower(n, 'A', 'B', 'C');  //n个盘子从A借助于B移动到C
    printf("总共移动了%d次!\n", count);
    return 0;
}

operation result:

insert image description here

Guess you like

Origin blog.csdn.net/gary101818/article/details/123287479