void toh(int n, char source, char dest, char temp) { if(n > 0) { toh(n-1, source, temp, dest); printf("Move disk %d from %c to %c\n", n, source, dest); toh(n-1, temp, dest, source); } }
void toh(int n,char S,char T,char D) { if(n==1) printf("move disk 1 from %c to %c\n",S,D); else { toh(n-1,'S','D','T'); printf("move disk %d from %c to %c\n",n,S,D); toh(n-1,'T','S','D'); } }
int main() { double h, t, d; double x, y; char a, b; while (scanf("%c %lf %c %lf\r\n", &a, &x, &b, &y) && a != 'E') { h = t = d = MAX; if (a == 'T') t = x; else if (a == 'D') d = x; else h = x; if (b == 'T') t = y; else if (b == 'D') d = y; else h = y; if (h == MAX) h = toh(t, d); else if (t == MAX) t = tot(d, h); else d = tod(t, h); printf("T %.1f D %.1f H %.1f\r\n", t, d, h); } }
int main(void) { int n; char source = 'A', dest = 'C', temp = 'B'; printf("Enter the no of disks: \n"); scanf("%d", &n); toh(n, source, dest, temp); }
void toh(Node *a,Node *b, Node *c, int n) { if(n==1) { int p=pop(a); push(b,p); } else { toh(a,c,b,n-1); int p=pop(a); push(b,p); toh(c,b,a,n-1); } }
main() { int n; char S,T,D; printf("enter the no. of disk to move from S to D using T as intermediate\t"); scanf("%d",&n); toh(n,'S','T','D'); getch(); }
main() { Node *a,*b,*c; a=(Node*)malloc(sizeof(Node)); b=(Node*)malloc(sizeof(Node)); c=(Node*)malloc(sizeof(Node)); a->next=b->next=c->next=0; int i,j; printf("Enter the number of digits of which tower of hannoi is to be solved. --> "); scanf("%d",&j); for(i=j;i>=1;i--) push(a,i); toh(a,b,c,j); display(b); }