Wednesday, March 24, 2010

ds - tower of honoi implementation

#include"stdio.h"
#include"conio.h"
void tower(int, char *,char *,char *,int *);
void main()
{
char *t1="source",*t2="auxiliary",*t3="destination";
int disk,step=0;
clrscr();
printf("Enter no. of disks:");
scanf("%d",&disk);
tower(disk,t1,t3,t2,&step);
getch();
}
void tower(int disk,char *t1,char *t3,char *t2,int *step)
{
if(disk==1) {
printf("\nstep %d:move %s to %s\n",*step+1,t1,t3);
(*step)++; }
else{
tower(disk-1,t1,t2,t3,step);
printf("\nstep %d:move %s to %s\n",*step+1,t1,t3);
(*step)++;
tower(disk-1,t2,t3,t1,step);
}
}

No comments:

Post a Comment