用递归实现汉诺塔

 如何理解汉诺塔的递归?

 

using namespace std;
#include <iostream>
#include <cstdio>

void move (int n, char from, char buffer, char to){
    if (n == 1) {
        cout << "Move" << n << " from " << from << " to " << to << endl;
    }
    else {
        move (n-1, from, to, buffer);
        move (1, from, buffer, to);
        move (n-1, buffer, from, to);
    }
}

 

posted @ 2020-06-13 19:21  洪豆豆的记录  阅读(197)  评论(0编辑  收藏  举报