30_hanoi.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* example from http://barnyard.syr.edu/quickies/hanoi.c */
  2. /* hanoi.c: solves the tower of hanoi problem. (Programming exercise.) */
  3. /* By Terry R. McConnell (12/2/97) */
  4. /* Compile: cc -o hanoi hanoi.c */
  5. /* This program does no error checking. But then, if it's right,
  6. it's right ... right ? */
  7. /* The original towers of hanoi problem seems to have been originally posed
  8. by one M. Claus in 1883. There is a popular legend that goes along with
  9. it that has been often repeated and paraphrased. It goes something like this:
  10. In the great temple at Benares there are 3 golden spikes. On one of them,
  11. God placed 64 disks increasing in size from bottom to top, at the beginning
  12. of time. Since then, and to this day, the priest on duty constantly transfers
  13. disks, one at a time, in such a way that no larger disk is ever put on top
  14. of a smaller one. When the disks have been transferred entirely to another
  15. spike the Universe will come to an end in a large thunderclap.
  16. This paraphrases the original legend due to DeParville, La Nature, Paris 1884,
  17. Part I, 285-286. For this and further information see: Mathematical
  18. Recreations & Essays, W.W. Rouse Ball, MacMillan, NewYork, 11th Ed. 1967,
  19. 303-305.
  20. *
  21. *
  22. */
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #define TRUE 1
  26. #define FALSE 0
  27. /* This is the number of "disks" on tower A initially. Taken to be 64 in the
  28. * legend. The number of moves required, in general, is 2^N - 1. For N = 64,
  29. * this is 18,446,744,073,709,551,615 */
  30. #define N 4
  31. /* These are the three towers. For example if the state of A is 0,1,3,4, that
  32. * means that there are three discs on A of sizes 1, 3, and 4. (Think of right
  33. * as being the "down" direction.) */
  34. int A[N], B[N], C[N];
  35. void Hanoi(int,int*,int*,int*);
  36. /* Print the current configuration of A, B, and C to the screen */
  37. void PrintAll()
  38. {
  39. int i;
  40. printf("A: ");
  41. for(i=0;i<N;i++)printf(" %d ",A[i]);
  42. printf("\n");
  43. printf("B: ");
  44. for(i=0;i<N;i++)printf(" %d ",B[i]);
  45. printf("\n");
  46. printf("C: ");
  47. for(i=0;i<N;i++)printf(" %d ",C[i]);
  48. printf("\n");
  49. printf("------------------------------------------\n");
  50. return;
  51. }
  52. /* Move the leftmost nonzero element of source to dest, leave behind 0. */
  53. /* Returns the value moved (not used.) */
  54. int Move(int *source, int *dest)
  55. {
  56. int i = 0, j = 0;
  57. while (i<N && (source[i])==0) i++;
  58. while (j<N && (dest[j])==0) j++;
  59. dest[j-1] = source[i];
  60. source[i] = 0;
  61. PrintAll(); /* Print configuration after each move. */
  62. return dest[j-1];
  63. }
  64. /* Moves first n nonzero numbers from source to dest using the rules of Hanoi.
  65. Calls itself recursively.
  66. */
  67. void Hanoi(int n,int *source, int *dest, int *spare)
  68. {
  69. int i;
  70. if(n==1){
  71. Move(source,dest);
  72. return;
  73. }
  74. Hanoi(n-1,source,spare,dest);
  75. Move(source,dest);
  76. Hanoi(n-1,spare,dest,source);
  77. return;
  78. }
  79. int main()
  80. {
  81. int i;
  82. /* initialize the towers */
  83. for(i=0;i<N;i++)A[i]=i+1;
  84. for(i=0;i<N;i++)B[i]=0;
  85. for(i=0;i<N;i++)C[i]=0;
  86. printf("Solution of Tower of Hanoi Problem with %d Disks\n\n",N);
  87. /* Print the starting state */
  88. printf("Starting state:\n");
  89. PrintAll();
  90. printf("\n\nSubsequent states:\n\n");
  91. /* Do it! Use A = Source, B = Destination, C = Spare */
  92. Hanoi(N,A,B,C);
  93. return 0;
  94. }
  95. /* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/