HANOÏ

cs_Joky Messages postés 1787 Date d'inscription lundi 22 novembre 2004 Statut Membre Dernière intervention 31 janvier 2009 - 27 nov. 2006 à 18:37
TecHunter Messages postés 3 Date d'inscription dimanche 22 octobre 2006 Statut Membre Dernière intervention 24 juillet 2007 - 5 déc. 2006 à 06:54
Cette discussion concerne un article du site. Pour la consulter dans son contexte d'origine, cliquez sur le lien ci-dessous.

https://codes-sources.commentcamarche.net/source/40483-hanoi

TecHunter Messages postés 3 Date d'inscription dimanche 22 octobre 2006 Statut Membre Dernière intervention 24 juillet 2007
5 déc. 2006 à 06:54
/*
voila un qu'on a fait en cours, par contre g encore un détail a régler avec l'affichage
*/
#include <stdio.h>
#define NBR_ETAGES 18

void affichage(int H[])
{
int i,j,k,l;

system("cls");

for(i=0;i<NBR_ETAGES;i++)
{
for(j=2;j<=H[i];j++)
printf("\t\t\t");
for(l=NBR_ETAGES-i;l>=0;l--)
printf(" ");
for(k=0;k<2*i+1;k++)
printf("*");

printf("\n");
}
}


void move(int H[],int rondelle,int destination)
{

int i,sous_destination=1;

for(i=rondelle-1;i>=0;i--)
{
if(H[i]==destination)
{
while(sous_destination==destination||sous_destination==H[i])
sous_destination++;

move(H,i,sous_destination);
affichage(H);
//system("pause");
}
if(H[i]==H[rondelle])
{
while(sous_destination==destination||sous_destination==H[rondelle])
sous_destination++;

move(H,i,sous_destination);
affichage(H);
//system("pause");
}
}
H[rondelle]=destination;
affichage(H);
//system("pause");
}

int main (void)
{
/* H , tableau dont les indices donnent la taille des rondelles
** , H[i] est la pique sur laquelle la rondelle i est positionée.
*/

int H[NBR_ETAGES],i;

//toutes les rondelles en 1
for(i=0;i<NBR_ETAGES;i++)
H[i]=1;

for(i=NBR_ETAGES-1;i>=0;i--)
move(H,i,3);

printf("fin\n");
system("pause");
return 1;
}
cs_Joky Messages postés 1787 Date d'inscription lundi 22 novembre 2004 Statut Membre Dernière intervention 31 janvier 2009 2
28 nov. 2006 à 22:29
Euh rectification au bout de 2 jours :)
La version itérative biensûr :D
cs_Joky Messages postés 1787 Date d'inscription lundi 22 novembre 2004 Statut Membre Dernière intervention 31 janvier 2009 2
27 nov. 2006 à 18:37
Et faut préciser que la version récursive est très jolie je trouve.
Rejoignez-nous