用栈
代码:
#include<iostream>
#include<stack>
using namespace std;
int res;
int n,m;
stack<int> st[4];//st[i]是从左到右的三个杆子,i为1 2 3
void hanoi(int n,int x,int y,int z){//把n个圆盘从x移到z
if(n==1) move(x,z,n);//不是return move(x,z,n)啊亲
else{
hanoi(n-1,x,z,y);
move(x,z,n);
hanoi(n-1,y,x,z);
}
}
void move(int x,int y,int n){//取x栈顶的盘子移给y,n为top
int t=st[x].top();
st[x].pop();
st[y].push(t);
res++;
//输出格式
if(res==m){
char a,b;
if(x==1) a='A';
if(x==2) a='B';
if(x==3) a='C';
if(y==1) b='A';
if(y==2) b='B';
if(y==3) b='C';
cout<<'#'<<m<<':'<<a<<"->"<<b<<endl;//杆子名字叫A B C
}
}
int main(){
cin>>n>>m;
for(int i=n;i>=1;i--){
st[1].push(i);
}
hanoi(n,1,2,3);
cout<<res;
return 0;
}
好像有点问题....