#include<stdio.h>
int main(){
long long i, res,t,k;int n,s;
scanf("%d", &n);
res=(1<<n)-1;
printf("%lld\n",res);
for( i=1; i <=res; i++ ){
for( t=2,s=1; s<= n; s++,t*=2)if( i%t == t/2 ) break;//i%t=t/2 找 第i步移动的S号盘
k = i/t;//获得第S盘 第几次移动
if( n%2 == s%2 ){// 逆时针
if( (k+1)%3 == 0 ) printf("%d from B to A\n",s);
if( (k+1)%3 == 1 ) printf("%d from A to C\n",s);
if( (k+1)%3 == 2 ) printf("%d from C to B\n",s);
}
else{// 逆时针
if( (k+1)%3 == 0 ) printf("%d from C to A\n",s);
if( (k+1)%3 == 1 ) printf("%d from A to B\n",s);
if( (k+1)%3 == 2 ) printf("%d from B to C\n",s);
}
}
return 0;
}
最美汉诺塔问题算法(非递归)(不用数据结构)
最新推荐文章于 2022-10-30 04:00:53 发布