用递归定义的蛮力算法,却已经是最优了。
#include <iostream>
//time:2022年8月19日17:42:22
//author:ZTusing namespace std;
int cc=0;
void movet(char A,char C,int n)
{
cc++;cout<<"从"<<A<<"到"<<C<<"转移一个"<<n<<"号圆盘"<<endl;
}
void hanoi(int n,char A,char B,char C)
{cout<<"把"<<n<<"个圆盘从"<<A<<"经过"<<B<<"转移到"<<C<<endl;
if(n>=2){
hanoi(n-1,A,C,B);movet(A,C,n);
hanoi(n-1,B,A,C);
}
else
{
movet(A,C,n);
}
}
int main()
{
cout << "Hello world!" << endl;
char A='A',B='B',C='C';
hanoi(11,A,B,C);
cout<<cc;
return 0;
}