fork download
  1. void hanoi(rod **head, rod **tail)
  2. {
  3. time_start();
  4.  
  5. rod *temp, *temp2, *head_t = *head;
  6. int flag = 0, div = 0;
  7.  
  8. for(int i = 0; i < rods - 2; i++)
  9. {
  10. while((*tail)->disks[n - 1] != n)
  11. {
  12. temp = *head, flag = 0;
  13.  
  14. if(div % 2 == 0) // move smallest disk to the right if odd, or left if even
  15. {
  16. while(temp != (*tail)->next)
  17. {
  18. if(flag == 1)
  19. {
  20. unshift(1, &temp->disks);
  21. break;
  22. }
  23. if(temp->disks[0] == 1)
  24. {
  25. pop(&temp->disks);
  26. flag = 1;
  27. }
  28.  
  29. if(n % 2 == 0)
  30. {
  31. temp = (temp->next == (*tail)->next) ? *head : temp->next;
  32. }
  33. else
  34. {
  35. temp = (temp->prev == (*head)->prev) ? *tail : temp->prev;
  36. }
  37. }
  38. }
  39. else // make first possible move
  40. {
  41. while(temp != (*tail)->next && flag == 0)
  42. {
  43. temp2 = *head;
  44. while(temp2 != (*tail)->next)
  45. {
  46. if((temp->disks[0] < temp2->disks[0] || temp2->disks[0] == 0) && temp->disks[0] != 1 && temp->disks[0] != 0 && temp->num != temp2->num)
  47. {
  48. int t = temp->disks[0];
  49. pop(&temp->disks);
  50. unshift(t, &temp2->disks);
  51. flag = 1;
  52. break;
  53. }
  54.  
  55. temp2 = temp2->next;
  56. }
  57.  
  58. temp = temp->next;
  59. }
  60. }
  61.  
  62. div++, moves++;
  63. }
  64.  
  65. // divide and conquer
  66. if(i < rods - 3)
  67. {
  68. if((*head)->num <= i + 1)
  69. *head = (*head)->next;
  70. if((*tail)->num <= i + 3)
  71. *tail = (*tail)->next;
  72. }
  73. }
  74.  
  75. *head = head_t;
  76.  
  77. time_stop();
  78. }
  79.  
  80. /**
  81.  * Input: {1, 2, 3, 4, 0}, el = 5
  82.  * Output: {5, 1, 2, 3, 4}
  83.  *
  84.  * Input array ALWAYS contains 0's at the end
  85.  */
  86. void unshift(int el, int **arr)
  87. {
  88. for(int i = (int)n - 1; i > 0; i--)
  89. {
  90. (*arr)[i] = (*arr)[i - 1];
  91. }
  92. (*arr)[0] = el;
  93. }
  94.  
  95. /**
  96.  * Input: {1, 2, 3, 4}
  97.  * Output: {2, 3, 4, 0}
  98.  */
  99. void pop(int **arr)
  100. {
  101. for(int i = 0; i < (int)n - 1; i++)
  102. {
  103. (*arr)[i] = (*arr)[i + 1];
  104. }
  105. (*arr)[n - 1] = 0;
  106. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1:12: error: variable or field ‘hanoi’ declared void
prog.cpp:1:12: error: ‘rod’ was not declared in this scope
prog.cpp:1:18: error: ‘head’ was not declared in this scope
prog.cpp:1:24: error: ‘rod’ was not declared in this scope
prog.cpp:1:30: error: ‘tail’ was not declared in this scope
stdout
Standard output is empty