Tower Of Hanoi

Tower Of Hanoi

Tower of Hanoi is a mathematical puzzle where we have three rods and n disks. The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules:

1) Only one disk can be moved at a time.
2) Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack.
3) No disk may be placed on top of a smaller disk



The solution may be stated as:-


1- Move top n-1 disks from A to B using C as an auxiliary.
2- Move the remaining disks from A to C.
3- Move n-1 disks from B to C using A as an auxiliary.



C code of Tower Of Hanoi.


#include<stdio.h>
int main()
{
    int a[]={5,4,3,2,1},b[5],c[5],aa=5,bb=0,cc=0,i=0,k=0,j=0,count=0,g=1,aaa=1,bbb=0,ccc=0;
    char d,s;
    printf("A : 5 4 3 2 1\nB :\nC :");
    while(k!=5)
    {
        k=0;
        g=1;
        printf("\n\n\n");
        while(g)
        {
        printf("Source : ");
            scanf(" %c",&s);
        if(s=='A'||s=='a')
        {
            if(aaa==0||(bbb!=0&&ccc!=0&&(a[aa-1]>b[bb-1]&&a[aa-1]>c[cc-1])))
               g=1;
            else
            {
                g=0;
            --aa;
            i=a[aa];
            if(aa==0)
                aaa=0;
            }
            
        }
        else if(s=='B'||s=='b')
        {
            if(bbb==0||(aaa!=0&&ccc!=0&&(b[bb-1]>a[aa-1]&&b[bb-1]>c[cc-1])))
                g=1;
            else
            {
                g=0;
            --bb;
            i=b[bb];
            if(bb==0)
                bbb=0;
            }
        }
        if(s=='C'||s=='c')
        {
            if(ccc==0||(aaa!=0&&bbb!=0&&(c[cc-1]>a[aa-1]&&c[cc-1]>b[bb-1])))
                g=1;
            else
            {
                g=0;
            --cc;
            i=c[cc];
            if(cc==0)
                ccc=0;
            }
        }
        }
        g=1;
        while(g)
        {
        printf("Destination : ");
        scanf(" %c",&d);
        if(d=='A'||d=='a')
        {
            if(a[aa-1]<i&&aaa!=0)
            g=1;
            else
            {
                aaa=1;
                g=0;
            a[aa]=i;
            ++aa;
            }
        }
        if(d=='B'||d=='b')
        {
            if(b[bb-1]<i&&bbb!=0)
            g=1;
            else
            {
                bbb=1;
                g=0;
            b[bb]=i;
            ++bb;
            }
        }
        if(d=='C'||d=='c')
        {
            if(c[cc-1]<i&&ccc!=0)
            g=1;
            else
            {
                ccc=1;
                g=0;
            c[cc]=i;
            ++cc;
            }
        }
        }
        printf("\n\n\nA : ");
        for(j=0;j<aa;j++)
            printf("%d ",a[j]);
        printf("\nB : ");
        for(j=0;j<bb;j++)
            printf("%d ",b[j]);
        printf("\nC : ");
        for(j=0;j<cc;j++)
        {
            if(c[j]==(j+1))
                k++;
            printf("%d ",c[j]);
        }
        count++;
        printf("\n\t\tMove : %d ",count);
    }
    if(k==5)
        printf("\n\nYou Completed Tower Of Hanoi in %d Moves\n\n\n",count);
}


Comments

Popular posts from this blog

CS50 IDE (Best Platform for coding Online/Offline)

Top Websites That Will Teach You Coding For Free

Best Apps to do Programming on Android Platform.