Friday 19 October 2012

TOWER OF HANOI


#include<stdio.h>
#include<conio.h>
void transfer(int n, char from, char to, char temp);
int main()
{
    int n;
    clrscr();
    printf("WELCOME TO THE TOWERS OF HONAI\n\n");
    printf("How many disks? ");
    scanf("%d", &n);
    printf("\n");
    transfer(n,'L','R','C');
    getch();
    return 0;
}
void transfer(int n, char from, char to, char temp){
    /** n = number of disks
        from = orinin
        to = destination
        temp = temporary storage  **/
    if(n>0){
        /** move n-1 disks from origin to temporary **/
        transfer(n-1, from, temp, to);
        /** move nth disk from origin to destination **/
        printf("Move disk %d from %c to %c\n", n, from, to);
        /** move n-1 disks from temporary to destination **/
        transfer(n-1, temp, to, from);
    }
}

No comments: