Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. #include<iostream>
  2. #define up '1'
  3. #define down '2'
  4. #define left '3'
  5. #define right '4'
  6.  
  7. using namespace std;
  8.  
  9. struct list_element
  10. {
  11. char letter;
  12. list_element* next;
  13. list_element* previous;
  14. list_element(char l, list_element* n, list_element* p) {
  15. letter = l;
  16. next = n;
  17. previous = p;
  18. }
  19. };
  20.  
  21. struct list
  22. {
  23. list_element* head;
  24. list_element* tail;
  25. int x;
  26. int y;
  27. char direction;
  28. list() {
  29. head = NULL;
  30. tail = NULL;
  31. direction = right;
  32. }
  33. };
  34.  
  35. void add_head(list* l, char letter)
  36. {
  37. if (l->head == NULL)
  38. {
  39. l->head = new list_element(letter, NULL,NULL);
  40. l->tail = l->head;
  41. }
  42. else {
  43. l->head->next = new list_element(letter, NULL, l->head);
  44. l->head = l->head->next;
  45. }
  46. }
  47.  
  48. void add_tail(list* l, char letter)
  49. {
  50. if (l->tail == NULL)
  51. {
  52. l->head = new list_element(letter, NULL, NULL);
  53. l->tail = l->head;
  54. }
  55. else {
  56. l->tail->previous = new list_element(letter, l->tail, NULL);
  57. l->tail = l->tail->previous;
  58. }
  59. }
  60.  
  61. char delete_head(list* l)
  62. {
  63. if (l->head == NULL)
  64. return ' ';
  65.  
  66. char letter = l->head->letter;
  67. if (l->head->previous == NULL)
  68. {
  69. delete l->head;
  70. l->head = NULL;
  71. l->tail = NULL;
  72. }
  73. else
  74. {
  75. list_element *temp = l->head;
  76. l->head = l->head->previous;
  77. l->head->next = NULL;
  78. delete temp;
  79. }
  80. return letter;
  81. }
  82.  
  83.  
  84. char delete_tail (list* l)
  85. {
  86. if (l->tail == NULL)
  87. return ' ';
  88.  
  89. char letter = l->tail->letter;
  90. if (l->tail->next == NULL)
  91. {
  92. delete l->tail;
  93. l->tail = NULL;
  94. l->head = NULL;
  95. }
  96. else
  97. {
  98. list_element *temp = l->tail;
  99. l->tail = l->tail->next;
  100. l->tail->previous = NULL;
  101. delete temp;
  102. }
  103. return letter;
  104. }
  105.  
  106. void write(list* l)
  107. {
  108. if (l->head == NULL)
  109. cout << "#" << endl;
  110. else
  111. {
  112. list_element * temp = l->head;
  113. while (temp != NULL)
  114. {
  115. cout << temp->letter;
  116. temp = temp->previous;
  117. }
  118. cout << endl;
  119. }
  120. }
  121.  
  122. void change_place(list* l, int width, int height)
  123. {
  124. if (l->direction == up) {
  125. l->y--;
  126. if (l->y == -1)
  127. l->y = height - 1;
  128. }
  129. else if(l->direction == down) {
  130. l->y++;
  131. if (l->y == height)
  132. l->y = 0;
  133. }
  134. else if (l->direction == left) {
  135. l->x--;
  136. if (l->x == -1)
  137. l->x = width -1;
  138. }
  139. else if (l->direction == right) {
  140. l->x++;
  141. if (l->x == width)
  142. l->x = 0;
  143. }
  144. }
  145.  
  146. void move_caterpillar(list* l, char**tab, int width, int height)
  147. {
  148. if (l->head == NULL)
  149. return;
  150. else
  151. {
  152. if (tab[l->y][l->x] == up)
  153. l->direction = up;
  154. else if (tab[l->y][l->x] == down)
  155. l->direction = down;
  156. else if (tab[l->y][l->x] == left)
  157. l->direction = left;
  158. else if (tab[l->y][l->x] == right)
  159. l->direction = right;
  160. else if (tab[l->y][l->x] >= 'a' && tab[l->y][l->x] <= 'z') {
  161. add_head(l, tab[l->y][l->x]);
  162. tab[l->y][l->x]--;
  163. }
  164. else if (tab[l->y][l->x] >= 'A' && tab[l->y][l->x] <= 'Z') {
  165. add_tail(l, tab[l->y][l->x]+' ');
  166. tab[l->y][l->x]--;
  167. }
  168. else if (tab[l->y][l->x] == 'a'-1)
  169. delete_head(l);
  170. else if (tab[l->y][l->x] == 'A' - 1)
  171. delete_tail(l);
  172.  
  173.  
  174. }
  175. }
  176.  
  177. int main()
  178. {
  179. list l;
  180. int height, width, lenght, moves;
  181. cin >> width >> height >> moves;
  182. cin >> l.x >> l.y >> lenght;
  183. l.x--;
  184. l.y--;
  185. char sign;
  186. for (int i = 0; i < lenght; i++)
  187. {
  188. cin >> sign;
  189. add_tail(&l, sign);
  190. }
  191. char** tab = new char*[height];
  192. for (int i = 0; i < height; i++)
  193. tab[i] = new char[width];
  194.  
  195. for (int i = 0; i < height; i++)
  196. {
  197. for (int j = 0; j < width; j++)
  198. {
  199. cin >> tab[i][j];
  200. }
  201. }
  202. move_caterpillar(&l, tab, width, height);
  203. for (int i = 0; i < moves-1; i++) {
  204. if (l.head == NULL)
  205. break;
  206. else {
  207. change_place(&l, width, height);
  208. move_caterpillar(&l, tab, width, height);
  209. }
  210. }
  211.  
  212. cout << l.x + 1 << " " << l.y + 1 << " ";
  213. write(&l);
  214. for (int i = 0; i < height; i++)
  215. {
  216. for (int j = 0; j < width; j++)
  217. {
  218. if (tab[i][j] == 'a' - 1)
  219. cout << '@';
  220. else
  221. cout << tab[i][j];
  222. }
  223. cout << endl;
  224. }
  225.  
  226. for (int i = 0; i < height; i++)
  227. delete[] tab[i];
  228.  
  229. delete[] tab;
  230.  
  231. while (l.head != NULL)
  232. delete_head(&l);
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement