home *** CD-ROM | disk | FTP | other *** search
- /*****************************************
- * hanoi.c --
- * Towers of Hanoi program as in _The Age of Intelligent Machines_
- * by Raymond Kurzweil with main routine written by Sameer Parekh
- * (zane@ddsw1.MCS.COM)
- * and with display routine by Sameer Parekh
- *****************************************/
-
-
- mvdisk(number, destination, position, max)
- int number, destination, max;
- int *position;
- {
- int i, j;
- position[number] = destination;
- /* Display set */
-
- printf("\n");
- for(i = 1; i < 4; i++) {
- printf("%d\t", i);
- for(j = max; j > 0; j--) {
- if (position[j] == i) {
- printf("%d ", j);
- }
- }
- printf("\n");
- }
- }
-
- void hanoi(original, destination, free, number_of_disks, position, max)
- int original, destination, free, number_of_disks, max;
- int *position;
- {
- if (number_of_disks == 1) {
- mvdisk(1, destination, position, max);
- return;
- }
-
- hanoi(original, free, destination, number_of_disks - 1, position, max);
-
- mvdisk(number_of_disks, destination, position, max);
-
- hanoi(free, destination, original, number_of_disks - 1, position, max);
-
- return;
- }
-
- init(position, max)
- int *position;
- int max;
- {
- int i;
- for(i = 1; i < (max + 1); i++) {
- position[i] = 1;
- }
- }
-
- int main(argc, argv)
- int argc;
- char **argv;
-
- {
- int *position;
- int max;
- int number;
- if (argc != 2) {
- printf("Usage: %s <number of disks>\n", argv[0]);
- exit(1);
- }
- number = atoi(argv[1]);
-
- if (number < 1) {
- puts("Argument not appropriate!");
- exit(1);
- }
- position = malloc(number);
- max = number;
- init(position, max);
-
- hanoi(1, 2, 3, number, position, max);
- }
-
-
-