Tower of Hanoi Using c ++
#include<iostream.h>
#include<conio.h>
void towers(int,char,char,char);
void main()
{
int n; //Declare the variables to be used
clrscr();//Get the input for number of disks
cout<<"enter the no of disks : ";
cin>>n;towers(n,'A','C','B'); //Call the function
getch();
}
void towers(int n,char from,char to,char aux)
{
if(n==1) // If there is only one disk
{
cout<<endl<<"move 1 from peg "<<from<<" to "<<to;return;
}
towers(n-1,from,aux,to); //Recursive Call
cout<<endl<<"move "<<n<<" from peg "<<from<<" to "<<to;
towers(n-1,aux,to,from);
}
Information
->Tower of Hanoi is a mathematical puzzle invented by a French Mathematician Edouard Lucas in 1883.
->The game starts by having few discs stacked in increasing order of size. The number of discs can vary, but there are only three pegs.
->The Objective is to transfer the entire tower to one of the other pegs. However you can only move one disk at a time and you can never stack a larger disk onto a smaller disk. Try to solve it in fewest possible moves.
Solution of tower of Hanoi :-
-To get a better understanding for the general algorithm used to solve the Tower of Hanoi, try to solve the puzzle with a small amount of disks, 3 or 4, and once you master that , you can solve The same puzzle with more discs with the following algorithm.Recursive Solution for the Tower of Hanoi with algorithm Let’s call the three peg Src(Source), Aux(Auxiliary) and st(Destination).
Move the top N – 1 disks from the Source to Auxiliary tower
Move the Nth disk from Source to Destination tower
Move the N – 1 disks from Auxiliary tower to Destination tower.
Transferring the top N – 1 disks from Source to Auxiliary tower can again be thought of as a fresh problem and can be solved in the same manner.
So once you master solving Tower of Hanoi with three disks, you can solve it with any number of disks with the above algorithm.
The puzzle is well known to students of Computer Science since it appears in virtually any introductory text on data structures or algorithms.
A function solve with four arguments (number of disks) and three pegs (source, intermediary and destination) could look like this.
Algorithms
Solve
(N, Src,
Aux, Dst)
If N is 0
If N is 0
Exit
Else
solve (N-1, Src, Dst, Aux)
Move from Src to Dst
Solve(N -1 , Aux, Src, Dst)
nice page
ReplyDeleteWhat is solution of tower of hanoi in Mathematics way ??
ReplyDelete
Deletethe recursive pattern can help us generate more numbers to find an explicit (non-recursive) pattern. Here's how to find the number of moves needed to transfer larger numbers of disks from post A to post C, remembering that M = the number of moves needed to transfer n-1 disks from post A to post C:
for 1 disk it takes 1 move to transfer 1 disk from post A to post C;
for 2 disks, it will take 3 moves: 2M + 1 = 2(1) + 1 = 3
for 3 disks, it will take 7 moves: 2M + 1 = 2(3) + 1 = 7
for 4 disks, it will take 15 moves: 2M + 1 = 2(7) + 1 = 15
for 5 disks, it will take 31 moves: 2M + 1 = 2(15) + 1 = 31
thanks for info...
Delete