注册 登录
编程论坛 C++教室

哪位高手帮我一下

A258 发布于 2008-03-13 18:49, 421 次点击
int i=0;
void main()
{  
    int m;
    cin>>m;
    cout<<"buzhou:";
    cout<<endl;
    hanoi(m,'A','B','C');
    out();
}
 void hanoi(int n,char one, char two, char three)
{   
    if(n==1)
    move(one,three);
    else
    {
    hanoi(n-1,one,three,two);
    move(one,three);
    hanoi(n-1,two,one,three);
    }
}
void move( char x, char y )
{
    cout<<"从"<< x<<"到"<<y ;
    i++;
    cout<<endl;
}
void out()
{
    cout<<"总共要搬"<<i<<"次";
    cout<<endl;
}
 
哪位高手帮我一下
谢谢了
其中的void hanoi(int n,char one, char two, char three) ;函数到底是怎样运行的啊
要详细的解答
1 回复
#2
sunkaidong2008-03-13 19:15
这是分治法的思路#include<iostream.h>
int i=0;
void hanoi(int,char,char,char);
void move(char,char);
void out();
void main()
{  
    int m;
    cin>>m;
    cout<<"buzhou:";
    cout<<endl;
    hanoi(m,'A','B','C');
    out();
}
void hanoi(int n,char one, char two, char three)
{   cout<<one<<two<<three<<endl;
    if(n==1)
    move(one,three);
    else
    {
    hanoi(n-1,one,three,two); //一通过三搬到二
    move(one,three); //把一搬到三
    hanoi(n-1,two,one,three); //把二上面的通过一搬到三
    }
}
void move( char x, char y )
{
    cout<<"从"<< x<<"到"<<y ;
    i++;
    cout<<endl;
}
void out()
{
    cout<<"总共要搬"<<i<<"次";
    cout<<endl;
}
。。。要向搬最后一个假定前面的n-1个已经搬了。。。
1