汉诺塔问题解析

来源:互联网 发布:路由器选择策略及算法 编辑:程序博客网 时间:2024/05/01 19:05

#include <iostream>
using namespace std;
void move(char a,char b){
 cout<<"From "<<a<<" to "<<b<<endl;
}
void nuota(int n,char a,char b,char c){
 if(n==1)move(a,c);
 else{
  nuota(n-1,a,c,b);
  move(a,c);
  nuota(n-1,b,a,c);
 }
}
int main(){
 int dish;
 cin>>dish;
 cout<<"The steps:"<<endl;
 nuota(dish,'A','B','C');//借助B将碟子从A移动到C
 return 0;
}

 

原创粉丝点击