试题 算法提高 汉诺塔

 

 

 

 

代码:

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int step=0;    
int n,m;
void hanoi(int n,char A,char B,char C){
    if(n>=1){
        hanoi(n-1,A,C,B);
        step++;
        if(m==step){
            printf("#%d: %c->%c\n",n,A,C);
        }
        hanoi(n-1,B,A,C);
    }
    
}
int main(){
    cin>>n>>m;
    hanoi(n,'A','B','C');
    cout<<(1<<n)-1<<endl;
    return 0;
}

 

posted @ 2020-10-04 15:29  sqsq  阅读(150)  评论(0编辑  收藏  举报