用递归思想解决汉诺塔问题

看完汉诺塔问题的代码后不是很理解,恰好看到以为知乎大神的讲解,受益匪浅。

#include<iostream>
using namespace std;
int step;
void move(int n,char X,char Z,char Y){
    if(n == 0){
        return;
    }
    move(n-1,X,Y,Z);
    step++;
    cout<<"n="<<n<<"  "<<step<<":"<<X<<"-->"<<Z<<endl;
    move(n-1,Y,Z,X);
}
int main(){
    int n;
    cout<<"input:";
    cin>>n;
    move(n,'A','C','B');
    return 0;
}

猜你喜欢

转载自blog.csdn.net/q354636996/article/details/84022326