Tue. Mar 19th, 2024

Divide et Impera – Hanoi algorithm

Towers of Hanoi problem – Method Divide et impera
BRIEF three rods A, B, C being located in the rod a disc size
different, arranged from bottom of the sea at the
small. Knowing that moves one disc at a time and that no
can move a larger disk over a small disk, to move all
n discs on the rod A rod B using the rod as aid C.

 

/* Problema Turnurilor din Hanoi - Metoda Divide et Impera
Se dau 3 tije A,B,C pe tija A aflandu-se n discuri de dimensiuni
diferite, aranjate de la baza spre varf de la cel mai la cel mai 
mic.Stiind ca se muta cate un disc la un moment dat si ca nu se
poate muta un disc mai mare peste un disc mai mic,sa se mute toate
cele n discuri de pe tija A pe tija B utilizand drept ajutor tija C.*/

#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>

void hanoi(int nr_discuri,int start,int stop,int ajutor)
{
	if(nr_discuri == 1)
		printf("Muta un disc de pe tija %d pe tija %d\n",start,stop);
	  else
	   {
		  hanoi(nr_discuri-1,start,ajutor,stop);
		  printf("Muta un disc de pe tija %d pe tija %d\n",start,stop);
		  hanoi(nr_discuri-1,ajutor,start,stop);
	   }
}

void main()
 {
	 int n;
	 printf("Dati nr de discuri:");scanf("%d",&n);
	 hanoi(n,1,2,3);
 }


34,327 total views, 1 views today