fork download
  1. /*
  2.   Daily Programmer Challenge #221: Word Snake
  3.   Sample Input: SHENANIGANS SALTY YOUNGSTER ROUND DOUBLET TERABYTE ESSENCE
  4.  
  5.   Sample Output:
  6.   SHENANIGANS
  7.   A
  8.   L
  9. T
  10.   YOUNGSTER
  11.   O
  12.   U
  13.   N
  14.   TELBUOD
  15.   E
  16.   R
  17.   A
  18.   B
  19.   Y
  20.   T
  21.   ESSENCE
  22. */
  23. #define MAXX 20
  24. #include <stdio.h>
  25. #include <iostream>
  26. #include <stdlib.h>
  27. using namespace std;
  28. #define BUFFER 200
  29. #define MAXY 25
  30.  
  31. struct word
  32. {
  33. char string[20] = {}; //assume no word is longer than 20 characters
  34. char length;
  35. char startX;
  36. char startY;
  37. char endX;
  38. char endY;
  39. };
  40.  
  41. static inline
  42. int getNewEndX (word* last, word* next, int flipped, char output[][MAXX])
  43. {
  44. next->startX = last->endX;
  45. next->startY = last->endY;
  46. next->endY = last->endY;
  47. int temp = last->endX + (flipped)*next->length - 2;
  48. if ((temp < MAXX) && (temp > 0))
  49. {
  50. next->endX = temp;
  51. for(int i = 1; i < next->length-1; i++)
  52. {
  53. if((output[last->endY][next->startX+(i*flipped)] != 0))
  54. {
  55. return 0;
  56. }
  57. output[last->endY][next->startX+(i*flipped)] = next->string[i];
  58. next->endX = next->startX+(i*flipped);
  59. }
  60. return 1;
  61. }
  62.  
  63. else
  64. return 0;
  65. }
  66.  
  67. static inline
  68. int getNewEndY (word* last, word* next, int flipped, char output[][MAXX])
  69. {
  70. next->startY = last->endY;
  71. next->startX = last->endX;
  72. next->endX = last->endX;
  73. int temp = last->endY + (flipped)*next->length - 2;
  74. if ((temp < MAXY) && (temp > 0))
  75. {
  76. next->endY = temp;
  77. for(int i = 1; i < next->length-1; i++)
  78. {
  79. if((output[last->endY + (i*flipped)][next->startX] != 0))
  80. {
  81. return 0;
  82. }
  83. output[last->endY + (i*flipped)][next->startX] = next->string[i];
  84. next->endY = next->startY+(i*flipped);
  85.  
  86. }
  87. return 1;
  88. }
  89.  
  90. else
  91. return 0;
  92. }
  93.  
  94. static inline
  95. int charBufferToWords(char input[], word word[])
  96. {
  97. char length = 0;
  98. char numwords = 0;
  99. char* pointer;
  100.  
  101. for(int i = 0; i < BUFFER; i++)
  102. {
  103. if (!(input[i] || input[i-1]))
  104. break;
  105.  
  106. length++;
  107. if((input[i] == ' ') || (input[i] == '\0'))
  108. {
  109.  
  110. word[numwords].length = length--;
  111. pointer = &input[i-length];
  112. for(int j = 0; j <= length; j++)
  113. {
  114. word[numwords].string[j] = *pointer++;
  115. }
  116. word[numwords].string[length] = '\0';
  117. numwords++;
  118. length = 0;
  119. }
  120.  
  121. }
  122. return numwords;
  123. }
  124.  
  125. int main ()
  126. {
  127. cout << "Enter a list of words to snake." << endl;
  128.  
  129. word word[20];
  130.  
  131. char output[MAXY][MAXX] = {0}; //Y, X
  132. char input[BUFFER] = {0};
  133.  
  134. gets(input);
  135. int numwords = charBufferToWords(input, word);
  136.  
  137. word[0].startX = 0;
  138. word[0].startY = 0;
  139.  
  140. word[0].endX = word[0].startX + word[0].length-2;
  141. word[0].endY = 0;
  142.  
  143. for(int i = 0; i < word[0].length; i++)
  144. {
  145. output[0][i] = word[0].string[i];
  146. }
  147.  
  148. int flipped = 1;
  149. for (int i = 1; i < numwords; i++) //start from the second word
  150. {
  151.  
  152. if (!getNewEndY(&word[i-1], &word[i], flipped, output))
  153. {
  154. flipped = -flipped;
  155. if(!getNewEndY(&word[i-1], &word[i], flipped, output))
  156. {
  157. exit(0);
  158. }
  159. }
  160.  
  161. ++i;
  162. if (i >= numwords)
  163. break;
  164. if (!getNewEndX(&word[i-1], &word[i], flipped, output))
  165. {
  166. flipped = -flipped;
  167. if(!getNewEndX(&word[i-1], &word[i], flipped, output))
  168. {
  169. exit(0);
  170. }
  171. }
  172. }
  173. cout << endl;
  174.  
  175. for (int i = 0; i < MAXY; i++)
  176. {
  177. for (int j = 0; j < MAXX; j++)
  178. {
  179. if (output[i][j] == 0)
  180. {
  181. putchar(' ');
  182. continue;
  183. }
  184. putchar(output[i][j]);
  185. }
  186. cout << endl;
  187. }
  188.  
  189. cout << endl;
  190.  
  191. return 0;
  192. }
  193.  
  194.  
Success #stdin #stdout 0s 3468KB
stdin
SHENANIGANS SALTY YOUNGSTER ROUND DOUBLET TERABYTE ESSENCE
stdout
Enter a list of words to snake.

SHENANIGANS         
          A         
          L         
          T         
          YOUNGSTER 
                  O 
                  U 
                  N 
            TELBUOD 
            E       
            R       
            A       
            B       
            Y       
            T       
            ESSENCE