设计一个递归算法求解汉诺塔问题
算了好久 囧
-
#include<iostream>
-
using namespace std;
-
-
void move(int n, int x, int y){
-
static int t = 1;
-
cout<<"第"<<t++<<"步 "<<char(x+64)<<"(第"<<n<<"号盘子) -> "<<char(y+64)<<endl;
-
}
-
-
void hanoi(int n,int a, int b, int c){
-
if(n==1)
-
move(1,a,c);
-
else{
-
hanoi(n-1,a,c,b);
-
move(n,a,c);
-
hanoi(n-1,b,a,c);
-
}
-
}
-
-
int main(){
-
int n;
-
-
cout<<"请输入第一根柱子上的盘子数:";
-
cin>>n;
-
-
hanoi(n,1,2,3);
-
-
return 0;
-
}