【例题 2-6】汉诺塔问题

描述

【题解】

之前写过一次详解:https://www.cnblogs.com/AWCXV/p/11395875.html

【代码】

#include <cstdio>
#define ll long long
using namespace std;

int cnt = 0;

//n个圆盘借助C柱移动到B柱
void move(int n,char a,char b,char c){
    if (n==0) return;//没有盘子要移动,直接结束
    move(n-1,a,c,b);//先把a柱子上面的n-1个盘子借助B柱子移动到C柱子
    printf("%d:%c->%c
",++cnt,a,b);//把a上面的剩余一个盘子直接放到B柱子上
    move(n-1,c,b,a);//把C柱子上的n-1个盘子再借助A柱子全部放到B柱子上
}

int main(){
    int n;
    scanf("%d",&n);
    move(n,'a','b','c');
    return 0;
}

原文地址:https://www.cnblogs.com/AWCXV/p/11619768.html