汉诺塔

#include <iostream>
using namespace std;

/**
 * 将前N个圆盘从from搬运到to通过辅助help
 * @param N
 * @param from
 * @param to
 */

void hanoi(int N, char from, char to, char help) {
    if(N == 1){     //如果N==1,直接移动
        cout<<"move "<<N<<" from "<<from<<" to "<<to<<endl;
        return;
    }
    hanoi(N - 1, from, help, to);   //将N-1个塔移动到辅助空间
    cout << "move " << N << " from " << from << " to " << to << endl;   //将第N个白塔移动到目标位置
    hanoi(N - 1, help, to, from);   //将剩余的N-1个塔移动到目标位置
}


int main() {
    hanoi(3,'a','c','b');
    return 0;
}
posted @ 2023-02-03 21:25  破忒头头  阅读(36)  评论(0)    收藏  举报