home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume16 / hanoi / part01 / hanoi.c next >
Encoding:
C/C++ Source or Header  |  1991-02-15  |  969 b   |  47 lines

  1. /*****************************************
  2.  * hanoi.c --
  3.  *  Towers of Hanoi program as in _The Age of Intelligent Machines_
  4.  *  by Raymond Kurzweil with main routine written by Sameer Parekh
  5.  *  (zane@ddsw1.MCS.COM)
  6.  *****************************************/
  7.  
  8. void hanoi(original, destination, free, number_of_disks)
  9. int original, destination, free, number_of_disks;
  10.  
  11. {
  12. if (number_of_disks == 1) {
  13.     printf("Move disk 1 from tower %d to tower %d\n", original, destination);
  14.     return;
  15.     }
  16.  
  17. hanoi(original, free, destination, number_of_disks - 1);
  18.  
  19. printf("Move disk %d from tower %d to tower %d\n", number_of_disks, original, destination);
  20.  
  21. hanoi(free, destination, original, number_of_disks - 1);
  22.  
  23. return;
  24. }
  25.  
  26. int main(argc, argv)
  27. int argc;
  28. char **argv;
  29.  
  30. {
  31. int number;
  32. if (argc != 2) {
  33.     printf("Usage: %s <number of disks>\n", argv[0]);
  34.     exit(1);
  35.     }
  36. number = atoi(argv[1]);
  37.  
  38. if (number < 1) {
  39.     puts("Argument not appropriate!");
  40.     exit(1);
  41.     }
  42.  
  43. hanoi(1, 2, 3, number);
  44. }
  45.  
  46.  
  47.