Write a program to solve the problem of tower of Hanoi with 3 pegs and N discs
#include
#include
class
hanoi
{
int
n;
char
peg_a, peg_b, peg_c;
public:
void
towers_hanoi(int n, char peg_a, char peg_b, char peg_c);
};
void
hanoi::towers_hanoi(int n, char peg_a, char peg_b, char peg_c)
{
if
(n<=0)
{
cout
<< "\nIllegal operation" ;
}
else
if (n==1)
{
cout
<< "\nMove disk from :\t" << peg_a <<
"\tto\t" << peg_c;
}
else
{
towers_hanoi(n-1,
peg_a, peg_c, peg_b);
towers_hanoi(1,
peg_a, peg_b, peg_c);
towers_hanoi(n-1,
peg_b, peg_a, peg_c);
}
}
void
main()
{
hanoi
obj;
int
n;
clrscr();
cout
<< "\nEnter numbers of disk :\t" ;
cin
>> n;
obj.towers_hanoi(n,
'A', 'B', 'C');
getch();
}
Comments
Post a Comment