123 Eng

Engineering the engineers™


Latest Jobs   Forum Map

 


Home
Source Codes
Engineering Colleges

Training  Reports
Seminar Reports
Placement Papers

Forums

   Computer Science / IT
   Electronics
   Electrical
   Mechanical
   Chemical
   Civil

   CAT / MBA

   GMAT / Foreign MBA
Latest Jobs

Engineering Jobs / Technical Jobs
Management Jobs

Sitemap
Terms of use

Displaying  Source Code(s)  
 

 
TOWER OF HANOI

--------------------------------------------------------------------------------

Description : solution of tower of hanoi

/*for any doubt any query mail me back ravindra@iitg.ernet.in*/
/*---------------sumitted by Ravindra kumar-------------------*/
/*-----------tested in Unix/Linux plateform-------------------*/

#include<iostream> //the c++ standard library for stream input output
#include<cstdio> //the c standard library for standard input output
#include<cstdlib> //for the exit function
using namespace std;

class arr //arr class that holds each stag
{
public:
int a[100],b[100],c[100];
int topa,topb,topc;
}hanoi;
int move=1; //counts the no. of moves
int main()
{
void tower(int ,int *,int *,int *,int *,int *,int *); //function
prototype
void show(int); //function prototype
int i;
system("clear");
cout<<"Enter the no of elements for which u want to solve the
problem<BR>;
scanf("%d",&i);
system("clear");
for(int j=0;j<i;j++) //feeds the elements in the arrs;
{
hanoi.a[j]=j+1;
hanoi.b[j]=-1;
hanoi.c[j]=-1;
}
hanoi.topa=i-1; //topa,topb,topc,mean the top of each arr
hanoi.topb=-1;
hanoi.topc=-1;
cout<<" Initially "<<"<BR>;
for(int j=i-1;j>-1;j--) //show the statusm of each arr
{
show(hanoi.a[j]);
show(hanoi.b[j]);
show(hanoi.c[j]);
cout<<"<BR>;
}
cout<<"

<BR>;

tower(i,hanoi.a,hanoi.c,hanoi.b,&(hanoi.topa),&(hanoi.topc),&(hanoi.topb))
; //call to do the job
}

void tower(int n,int src[],int dest[],int aux[],int *ts,int *td,int
*ta)
//the tower function passes
{ //the arrs along with the top pointers
void show (int);
if(n==1) //if one element is there in source arr ,
{ //then it is moved to the destination arr,
dest[++(*td)]=src[(*ts)];
src[*ts]=-1;
(*ts)--;
int max;
max=((*ts)>(*td)?(*ts):(*td));
max=(max>(*ta)?max:(*ta));
cout<<" Move "<<move++<<"<BR>;
for(int i=max;i>-1;i--) //status of arrs shown
{
show(hanoi.a[i]);
show(hanoi.b[i]);
show(hanoi.c[i]);
cout<<"<BR>;
}
cout<<"

<BR>;
return;
}
tower(n-1,src,aux,dest,ts,ta,td); //else the
tower(1,src,dest,aux,ts,td,ta); // problem is solved by
tower(n-1,aux,dest,src,ta,td,ts); // recursive calls

}

void show(int a) //the show function shows the current status of
the
arrs
{
if(a==-1)
cout<<"- ";
else
cout<<a<<" ";
}

 

 

 

Contribute content or training reports / feedback / Comments
job placement papers
All rights reserved © copyright 123ENG