slove(num,A,b,C)表示把1~num的盘子从A经过B移动到C
首先把solve(num-1,A,C,B)
然后移动num到C
最后solve(num-1,B,A,C)
即可
代码:
#include<iostream> #include<cstdio> #include<cmath> #define Size 21 using namespace std; int p[Size]; int n; inline char func(int x){ return char(x-1+'A'); } void solve(int num,int A,int B,int C){//把1~num从A经过B移动到C if(num<1)return; solve(num-1,A,C,B); p[num]=C; //cout<<num<<" from "<<func(A)<<" to "<<func(C)<<endl; printf("%d from %c to %c ",num,func(A),func(C)); solve(num-1,B,A,C); } int main(){ cin>>n; for(int i=1;i<=n;i++){ p[i]=1; } cout<<pow(2,n)-1<<endl;//steps solve(n,1,2,3); return 0; }