toh1

Upload: jagruti-boraste

Post on 06-Apr-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/2/2019 toh1

    1/3

    #include#include#include

    #defineMAX50#defineEMPTY0#defineDONE(pos_matrix[0][disks-1]==pos_matrix[1][disks-1])

    charnames[3]={A,B,C};intdisks;intpos_matrix[3][MAX];intsmallest_location=0;unsignedmove_no=0;

    intmain(int,char**);voidmove_disk(int,int);voiditowers(intdisks);intnext_disk(void);char*g_time(void);char*g_date(void);char*time_diff(char*t1,char*t2);

    intmain(intargc,char*argv[]){

    inti,j;

    char*run_date="";char*start_time="";char*finish_time="";char*time_difference="";

    if(argc!=2){printf("Usage:%s[n]where1

  • 8/2/2019 toh1

    2/3

    time_difference=time_diff(finish_time,start_time);printf("\nSolvedin%umoves.\n",move_no);printf("\nRundate=%s",run_date);printf("\nStartTime=%s",start_time);printf("\nFinishTime=%s",finish_time);printf("\nTimetaken=%s",time_difference);return(0);}

    voiditowers(intdisks){intdirection,temp;

    direction=(disks&1)?-1:1;

    while(!DONE){if(++move_no&1){temp=smallest_location;smallest_location+=direction;if(smallest_location2)

    smallest_location=0;

    move_disk(temp,smallest_location);}else{temp=next_disk();move_disk(temp,3-temp-smallest_location);}}}

    intnext_disk(void)

    {#defineSIZE0#definePOST1

    registerlocation;intpost,avl[2][2],disk=0;

    for(post=0;post

  • 8/2/2019 toh1

    3/3

    voidmove_disk(intsrc,intdst){registeri=0,j=0;

    while(pos_matrix[src][i]==EMPTY)i++;while(pos_matrix[dst][j]==EMPTY&&j