TOWERS OF HANOI
Posted by Vinod on December 21, 2006
/**************************************************************************
-> This C++ program is to solve the towers of hanoi problem.
-> Implemented using recursion.
-> Works in Microsoft VC++ 6.0 , windows xp.
-> Header files used 1)iostream.h
***************************************************************************/
#include<iostream.h>
void move(int n,char *s,char *i,char *d)
// s stands for source tower
// d stands for destination tower
// i stands for intermediate tower
{
if(n>0)
{
move(n-1,s,d,i);
// move n-1 disks from source to intermediate tower
cout<<”disk “<<n<<” is moved from “<<s<<” to “<<d<<endl;
// move the disk from to source to destination
move(n-1,i,s,d);
// move n-1 disks from intermediate to destination
}
}
void main()
{
cout<<”\n**********************************************************\n”;
cout<<”This C++ program is to solve the towers of hanoi problem”;
cout<<”\n**********************************************************\n”;
cout<<”Enter the no. of disks “;
int n;
cin>>n;
move(n,”source tower”,”intermediate tower”,”destination tower”);
}
kiran said
great prog. dude ..thanks … can u post the explanation also ..
i m new to recursion tech.
jaya ganesh said
c using linked list in towers of honai
me-ann said
Goodness,
thank God you posted such program… you know i do really need it on our major subject, it is just difficult for me to understand…but thanks anyway for the post…it helped me so much…
priya said
thanx alot 4r ur this post……….really helpfull to many like me who r new to recursion……..
can u explain me dis……..??
vike said
i need solution for “TOWER OF HANOI” in data structure & algorithm…for my assignment
hodenkrebs said
hey i just downt find a quelltext
jayquez said
thank you so much for making this program..it really help me in my assingment..thank God..i cant find it anwhere,only here..thank you!!!
glen john pazzibugan said
fuck you bitch im glen john from phillipines
glen john pazzibugan said
fuck you bitch im glen john from phillipines is this tower of nuynoy??
ezhrihm cradan said
youre the bitch!
shivaji mali said
Thanks for this program. Please guide me for my future problem
jebakumar said
thank you
thirudurgam said
tremands idea to rectfiy our problems
dhar said
could sumone tell me de solution for tower of hanoi without use of recursion….using iteration??plsssssssss
Disha said
thanks a lot sir..!! i relly needed this program
me said
i need tower of hanoi solution in c++ with graphics ………..
plz help me out
jugal said
have u a graphical representation of tower of hanoi in ds?
plz reply.
it is important for me.
i want these in 2 days.
Arin said
oh… it really helped me a lot… i have xam 2mrw n i had no idea of it… n its ma major course too…
VillenGOK said
thanks for only uuuuuuuuuu.
realy help with me , in this…………
jeevitha said
thanks for your program!!!!!!
mohit said
include
void move(int n,char *s,char *i,char *d)
// s stands for source tower
// d stands for destination tower
// i stands for intermediate tower
{
if(n>0)
{
move(n-1,s,d,i);
// move n-1 disks from source to intermediate tower
cout<<”disk “<<n<<” is moved from “<<s<<” to “<<d<<endl;
// move the disk from to source to destination
move(n-1,i,s,d);
// move n-1 disks from intermediate to destination
}
}
void main()
{
cout<<”\n**********************************************************\n”;
cout<<”This C++ program is to solve the towers of hanoi problem”;
cout<<”\n**********************************************************\n”;
cout<>n;
move(n,”source tower”,”intermediate tower”,”destination tower”);
}
Andrew said
Am giving a projest title An algorithm of cyclic tower of Hanoi.
Please how do I go about it?
praveen said
wat r the values of s,i,d???
sunil said
‘Towers of Hanoi’ Puzzle is explained in great detail with both the
- Recursive & Non-Recursive solution and
- Time & Space complexities
at the below link
http://www.rawkam.com/?p=917
Nikhil said
can u explain why have you used pointers while taking tower…… I mean what is the purpose for using that…. Reply soon….
Neenu said
thank u so muchhhhhhh………………………….
Neenu from Kannur(pappinisseri)
Neenu said
thank you so much dudeeeeeeeeeee
Neenu from Kannur(pappinisseri)
Neenu.p said
Thank you so much babaaaaaaaaa
Neenu from Kannur(pappinisseri)
Neenu.p said
And also i want algorithm of TOWER OF HANOI
SO plsss how do i go about it???????
NEENU FROM KANNUR(PAPPINISSERI)
Sudip said
1.Algorithm TowersOfHanoi(n,x,y,z)
2.//Move the top n diska from tower x to y using tower z
3.{
4. if(n>=1)
5. {
6. TowersOfHanoi(n-1,x,z,y);
7. write(“Move top disk from tower”,x,”to tower”,y);
8. TowersOfHanoi(n-1,z,y,x);
9. }
10.}
kirankumar said
you are also provide explanation
Tekalign T. said
10Q U very much!!
dee said
this is good
dee said
ye sub faltu hai koi mat padna
WSiaB said
Made code work cross-platform (debugged):
/* Tower of Hanoi */
#include
using namespace std;
void move (int ringNum, char *first, char *second, char *third)
{
if (ringNum > 0)
{
move(ringNum-1, first, third, second); // move ringNum-1 disks from 1st tower to 2nd tower
cout << "Disk " << ringNum << " is moved from " << first << " tower to " << third << " tower." << endl;
// move the disk from to 1st input (*first) to 3rd input (*third)
move(ringNum-1, second, first, third); // move ringNum-1 disks from 2nd tower to 3rd tower
}
return;
}
int main()
{
int ringNum = 0;
cout <> ringNum;
move (ringNum, "1st", "2nd", "3rd");
return 0;
}
WSiaB said
LOL, the site messed up my code worse than the original…let’s see if this is any better…
WSiaB said
One last try:
/* Tower of Hanoi */ #include <iostream> using namespace std; void move (int ringNum, char *first, char *second, char *third) { if (ringNum > 0) { move(ringNum-1, first, third, second); // move ringNum-1 disks from 1st tower to 2nd tower cout << "Disk " << ringNum << " is moved from " << first << " tower to " << third << " tower." << endl; // move the disk from to 1st input (*first) to 3rd input (*third) move(ringNum-1, second, first, third); // move ringNum-1 disks from 2nd tower to 3rd tower } return; } int main() { int ringNum = 0; cout << "Enter the # of disks: "; cin >> ringNum; move (ringNum, "1st", "2nd", "3rd"); return 0; }kundan said
which data structures used in Tower of Hanoi
how to calculate time compleixity in TOH…
can i implement using gready method in tower of hanoi prog….
Adil said
if you please write the same program in ‘c’ language using the ‘c’ syntax;
shoaib said
i m commanded by my teacher to show the visualize of tower of henoi using data structure(“c+”)..plz tell me how can i do that? and which will be the code for that of using GUI?
mark said
Dude! Look at may 7th 2011 its in c++. You must be extremely new to programming and this is already in recursion. If you want it in a more basic data structure it would be even simpler then this. So if your posting this kind of work for a basic data structure your instructor will know the difference. This is recursion which is n -1 which uses a tree. and the variable ringNum which is the max amount of rings and the bottom and 1 which is the least is at your top.
Stalin said
great code…simple and logical
Manish Kumar said
#include
#include
main()
{
int n;
void tower(char,char,char,int);
printf(“Enter the number of disk”);
scanf(“%d”,&n);
tower(‘i’,'j’,'k’,n);
getch();
}
void tower(char a,char b,char c,int n)
{
if(n==1)
printf(“\nMove disk from %c to %c “,a,c);
else
{
tower(a,c,b,n-1);
tower(a,b,c,1);
tower(b,a,c,n-1);
}
}
kul anand said
i want algorithm for this
kul anand said
plz i want in simplest logical way