例:
有n个大小不等的圆盘,有abc三根柱子,圆盘由下往上由小到大套在a柱上,现在要把圆盘由下往上由大到小放置问最少移动多少次
#include<stdio.h>
int step;
void Hanoi(int n,char a,char b,char c){
if(n==1){
step++;
printf("[step %d] plate%d %c -> %c\n",step,1,a,c);
}else{
Hanoi(n-1,a,c,b);
step++;
printf("[step %d] plate%d %c -> %c\n",step,n,a,c);
Hanoi(n-1,b,a,c);
}
}
int main() {
int n;
scanf("%d",&n);
Hanoi(n,'a','b','c');
printf("%d",step);
return 0;
}
假设有4个圆盘
测试结果