Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #define up '1'
- #define down '2'
- #define left '3'
- #define right '4'
- using namespace std;
- struct list_element
- {
- char letter;
- list_element* next;
- list_element* previous;
- list_element(char l, list_element* n, list_element* p) {
- letter = l;
- next = n;
- previous = p;
- }
- };
- struct list
- {
- list_element* head;
- list_element* tail;
- int x;
- int y;
- char direction;
- list() {
- head = NULL;
- tail = NULL;
- direction = right;
- }
- };
- void add_head(list* l, char letter)
- {
- if (l->head == NULL)
- {
- l->head = new list_element(letter, NULL,NULL);
- l->tail = l->head;
- }
- else {
- l->head->next = new list_element(letter, NULL, l->head);
- l->head = l->head->next;
- }
- }
- void add_tail(list* l, char letter)
- {
- if (l->tail == NULL)
- {
- l->head = new list_element(letter, NULL, NULL);
- l->tail = l->head;
- }
- else {
- l->tail->previous = new list_element(letter, l->tail, NULL);
- l->tail = l->tail->previous;
- }
- }
- char delete_head(list* l)
- {
- if (l->head == NULL)
- return ' ';
- char letter = l->head->letter;
- if (l->head->previous == NULL)
- {
- delete l->head;
- l->head = NULL;
- l->tail = NULL;
- }
- else
- {
- list_element *temp = l->head;
- l->head = l->head->previous;
- l->head->next = NULL;
- delete temp;
- }
- return letter;
- }
- char delete_tail (list* l)
- {
- if (l->tail == NULL)
- return ' ';
- char letter = l->tail->letter;
- if (l->tail->next == NULL)
- {
- delete l->tail;
- l->tail = NULL;
- l->head = NULL;
- }
- else
- {
- list_element *temp = l->tail;
- l->tail = l->tail->next;
- l->tail->previous = NULL;
- delete temp;
- }
- return letter;
- }
- void write(list* l)
- {
- if (l->head == NULL)
- cout << "#" << endl;
- else
- {
- list_element * temp = l->head;
- while (temp != NULL)
- {
- cout << temp->letter;
- temp = temp->previous;
- }
- cout << endl;
- }
- }
- void change_place(list* l, int width, int height)
- {
- if (l->direction == up) {
- l->y--;
- if (l->y == -1)
- l->y = height - 1;
- }
- else if(l->direction == down) {
- l->y++;
- if (l->y == height)
- l->y = 0;
- }
- else if (l->direction == left) {
- l->x--;
- if (l->x == -1)
- l->x = width -1;
- }
- else if (l->direction == right) {
- l->x++;
- if (l->x == width)
- l->x = 0;
- }
- }
- void move_caterpillar(list* l, char**tab, int width, int height)
- {
- if (l->head == NULL)
- return;
- else
- {
- if (tab[l->y][l->x] == up)
- l->direction = up;
- else if (tab[l->y][l->x] == down)
- l->direction = down;
- else if (tab[l->y][l->x] == left)
- l->direction = left;
- else if (tab[l->y][l->x] == right)
- l->direction = right;
- else if (tab[l->y][l->x] >= 'a' && tab[l->y][l->x] <= 'z') {
- add_head(l, tab[l->y][l->x]);
- tab[l->y][l->x]--;
- }
- else if (tab[l->y][l->x] >= 'A' && tab[l->y][l->x] <= 'Z') {
- add_tail(l, tab[l->y][l->x]+' ');
- tab[l->y][l->x]--;
- }
- else if (tab[l->y][l->x] == 'a'-1)
- delete_head(l);
- else if (tab[l->y][l->x] == 'A' - 1)
- delete_tail(l);
- }
- }
- int main()
- {
- list l;
- int height, width, lenght, moves;
- cin >> width >> height >> moves;
- cin >> l.x >> l.y >> lenght;
- l.x--;
- l.y--;
- char sign;
- for (int i = 0; i < lenght; i++)
- {
- cin >> sign;
- add_tail(&l, sign);
- }
- char** tab = new char*[height];
- for (int i = 0; i < height; i++)
- tab[i] = new char[width];
- for (int i = 0; i < height; i++)
- {
- for (int j = 0; j < width; j++)
- {
- cin >> tab[i][j];
- }
- }
- move_caterpillar(&l, tab, width, height);
- for (int i = 0; i < moves-1; i++) {
- if (l.head == NULL)
- break;
- else {
- change_place(&l, width, height);
- move_caterpillar(&l, tab, width, height);
- }
- }
- cout << l.x + 1 << " " << l.y + 1 << " ";
- write(&l);
- for (int i = 0; i < height; i++)
- {
- for (int j = 0; j < width; j++)
- {
- if (tab[i][j] == 'a' - 1)
- cout << '@';
- else
- cout << tab[i][j];
- }
- cout << endl;
- }
- for (int i = 0; i < height; i++)
- delete[] tab[i];
- delete[] tab;
- while (l.head != NULL)
- delete_head(&l);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement