简单的递归入门
move(n,from,buffer,to)表示把n个圆盘从from以buffer为中间柱移到to
#include <cstdio>
#include <iostream>
using namespace std;
void move(int n,char from,char buffer,char to);
int main(){
int T;
scanf("%d",&T);
while(T--){
int n;
scanf("%d",&n);
cout << (1 << n) - 1 << endl;
move(n,'A','B','C');
}
return 0;
}
void move(int n,char from,char buffer,char to){
if (n == 1){
printf("%c -> %c\n",from,to);//如果只剩一个圆盘,直接移
}else{
move(n-1,from,to,buffer);//把除最大圆盘外的n-1个从from移到buffer,以便把最大圆盘移到to
printf("%c -> %c\n",from,to);//把最大圆盘移到to
move(n-1,buffer,from,to);//把剩下的n-1个圆盘从buffer移到to
}
}