Tower of Hanoi recursion problem

Tower of Hanoi recursion is a classic problem that uses recursive thinking. I still don't fully understand it when I read the related ideas, but I will put the code on the blog first and slowly digest this problem.
# include <stdio.h>

void hannuota(int n, char A, char B, char C)//指将A上的盘子借助B移到C
{
    /*
     如果是1个盘子
     直接将A柱子上的盘子从A移到C
     否则
     先将A柱子上的n-1个盘子借助C移到B
     直接将A柱子上的盘子从A移到C
     最后将B柱子上的n-1个盘子借助A移到C
     最上面盘子为1最下面为n
     */
    if (1 == n)
    {
        printf("将编号为%d的盘子直接从%c柱子移到%c柱子\n", n, A, C);
    }
    else
    {
        hannuota(n-1, A, C, B);
        printf("将编号为%d的盘子直接从%c柱子移到%c柱子\n", n, A, C);
        hannuota(n-1, B, A, C);
    }
}

int main(void)
{
    int n;
    printf("请输入要移动盘子的个数: ");
    scanf("%d", &n);   
    hannuota(n, 'A', 'B', 'C');
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324778468&siteId=291194637