J'ai une question sur les tours linéaires de Hanoi. Je l'ai implémenté en C++ mais j'essaie de faire la même chose en utilisant la méthode récursive de queue ou itérative. J'ai des problèmes avec mon algorithme.tours linéaires de Hanoi
Cet extrait de code montre le transfert de blocs de la tour centrale à la tour d'extrémité.
#include <stdlib.h>
#include <stdio.h>
using namespace std;
//int a[5]={2,3,1,2,1};
int from,spare,to;
int main()
{
//int n;
//void hanoi(int,int,int,int);
void linear_hanoi(int,int,int,int);
void mid_to_end(int,int,int,int);
void end_to_mid(int,int,int,int);
//mid_to_end(3,2,3,1);
end_to_mid(4,3,2,1);
getchar();
return 0;
}
void linear_hanoi(int n, int from, int to, int spare)
{
if(n>0)
{
linear_hanoi(n-1,from,to,spare);
cout<<"move ring "<<n<<" from tower "<<from<<" to tower "<<spare<<endl;
linear_hanoi(n-1,to,from,spare);
cout<<"move ring "<<n<<" from tower "<<spare<<" to tower "<<to<<endl;
linear_hanoi(n-1,from,to,spare);
}
}
void mid_to_end(int n, int from, int to, int spare)
{
if(n>0)
{
mid_to_end(n-1,from,spare,to);
cout<<"move ring "<<n<<" from tower "<<from<<" to tower "<<to<<endl;
// mid_to_end(n-1,spare,from,to);
// mid_to_end(n-1,from,to,spare);
//cout<<"move ring "<<n<<" from tower "<<spare<<" to tower "<<from<<endl;
// mid_to_end(n-1,from,to,spare);
//cout<<"move ring "<<n<<" from tower "<<spare<<" to tower "<<from<<endl;
}
}
Qu'est-ce que je fais mal?
Est-ce que ce travail est fait? – jprete
Même si ce n'est pas le cas, c'est le devoir de quelqu'un; le marquer comme tel. –
non je supposé d'écrire un document de recherche. .comparing à la fois la récursivité et la récursivité de la queue – user181266