Friday, March 25, 2011

Tower of Hanoi

Tower  :                     x                  z                  y
Solution of Above Tower of Hanoi Problem will be soon..... using c++

#include<conio.h>
#include<iostream.h>
class tower
{
    //Private variables
        int NoDisk;
        char FromTower,ToTower,AuxTower;
    public:
        void hanoi(int,char,char,char);
};
void tower::hanoi(int NoDisk,char FromTower,char ToTower, char AuxTower)
{
     //if only one disk, make the move and return
        if (NoDisk == 1)
        {
            cout<<"\nMove from disk 1 from tower "<<FromTower<<" to tower "<<ToTower;
            return;
        }
            //Move top n1 disks from X to Y, using Z as aux. tower
       
        hanoi(NoDisk - 1,FromTower,AuxTower,ToTower);
       
  //Move remaining disk from X to Z      
        cout<<"\nMove from disk "<<NoDisk<<" from tower "<<FromTower<<" to tower "<<ToTower;
       
  //Move n1 disk from Y to Z using X as aux. tower
        hanoi(NoDisk - 1,AuxTower,ToTower,FromTower);
        return;
       
}

void main()
{
    int No;
    tower Ob;
    clrscr();
    cout<<"\n\t\t\t--- Tower of Hanoi ---\n";

  //Imput the number of disk in the tower
    cout<<"\n\nEnter the number of disks = ";
    cin>>No;

  //We assume that the towers are X, Y and Z
    Ob.hanoi(No,'X','Z','Y');

    getch();
   
}
Output :

Another Solution without recursion Technique :






   Move    Binary Number Interpretation
100001Move disk 1 to empty peg.
200010Move disk 2 to empty peg.
300011Move disk 1 to cover disk 2.
400100Move disk 3 to empty peg.
500101   Move disk 1 NOT to cover disk 3. 
600110Move disk 2 to cover disk 3.
700111Move disk 1 to cover disk 2.
801000Move disk 4 to empty peg.
901001Move disk 1 to cover disk 4.
1001010Move disk 2 NOT to cover disk 4.
1101011Move disk 1 to cover disk 2.
1201100Move disk 3 to cover disk 4.
1301101Move disk 1 NOT to cover disk 3.
1401110Move disk 2 to cover disk 3.
1501111Move disk 1 to cover disk 2.
1610000Move disk 5 to empty peg.
1710001Move disk 1 NOT to cover disk 5.
1810010Move disk 2 to cover disk 5.
1910011Move disk 1 to cover disk 2.
2010100Move disk 3 NOT to cover disk 5.
2110101Move disk 1 NOT to cover disk 3.
2210110Move disk 2 to cover disk 3.
2310111Move disk 1 to cover disk 2.
2411000Move disk 4 to cover disk 5.
2511001Move disk 1 to cover disk 4.
2611010Move disk 2 NOT to cover disk 4.
2711011Move disk 1 to cover disk 2.
2811100Move disk 3 to cover disk 4.
2911101Move disk 1 NOT to cover disk 3.
3011110Move disk 2 to cover disk 3.
3111111Move disk 1 to cover disk 2.

 
Think Above Solution for Five Ring Tower. and Consider Cases As Below Detail.
Complexity Consider on (2^5 -1)=31




   Move    Binary Number Interpretation
801000Move disk 4 to empty peg.
1301011Move disk 1 to cover disk 2.
2511001Move disk 1 to cover disk 4.
1810010Move disk 2 to cover disk 5.
1011010   Move disk 2 NOT to cover disk 4. 
1710001Move disk 1 NOT to cover disk 5.

1 comment:

  1. Its very nice.
    what is pro. by just using c.Gambhva sir tell me that it is possible to make this progame by using recursion in just 10line. so u find the solution than plz upload it.

    ReplyDelete