Advertisement
Guest User

Untitled

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