Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.74 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <limits.h>
  3. #include <stdlib.h>
  4.  
  5. #define MAX_SIZE 2000
  6. #define BUFFER_SIZE 2100
  7.  
  8. typedef struct Point {
  9.     int x;
  10.     int y;
  11. } Point;
  12.  
  13. typedef struct QueueNode {
  14.     Point p;
  15.     struct QueueNode *next;
  16. } QueueNode;
  17.  
  18. typedef struct Queue {
  19.     int size;
  20.     QueueNode *head;
  21.     QueueNode *tail;
  22. } Queue;
  23.  
  24. Point create_point(int x, int y) {
  25.     Point p;
  26.     p.x = x;
  27.     p.y = y;
  28.     return p;
  29. }
  30.  
  31. QueueNode *create_node(int x, int y) {
  32.     QueueNode* node = (QueueNode *) malloc(sizeof(QueueNode));
  33.     node->p = create_point(x, y);
  34.     node->next = NULL;
  35.     return node;
  36. }
  37.  
  38. Queue *create_queue() {
  39.     Queue *queue = (Queue *) malloc(sizeof(Queue));
  40.     queue->size = 0;
  41.     queue->head = create_node(0, 0);
  42.     queue->tail = create_node(0, 0);
  43.     queue->head->next = queue->tail;
  44.     return queue;
  45. }
  46.  
  47. void push(int x, int y, Queue *q) {
  48.     QueueNode* temp = q->head;
  49.     for (int i = 0; i < q->size; i++)
  50.         temp = temp->next;
  51.    
  52.     QueueNode* node = create_node(x, y);
  53.     node->next = temp->next;
  54.     temp->next = node;
  55.  
  56.     q->size++;
  57. }
  58.  
  59. Point pop(Queue *q) {
  60.     QueueNode *temp = q->head->next;
  61.     q->head->next = temp->next;
  62.    
  63.     Point data = temp->p;
  64.  
  65.     free(temp);
  66.     q->size--;
  67.  
  68.     return data;
  69. }
  70.  
  71. int isEmpty(Queue *q) {
  72.     return q->size == 0;
  73. }
  74.  
  75. int N, M;
  76.  
  77. int checkPoint(Point *p) {
  78.     return p->x >= 0 && p->y >= 0 && p->x < N && p->y < M;
  79. }
  80.  
  81. void printSolution(int L[N][M], Point p) {
  82.     if (p.x == 0 && p.y == 0) {
  83.         return;
  84.     }
  85.    
  86.     Point points[4] = {
  87.         create_point(p.x, p.y - 1),
  88.         create_point(p.x, p.y + 1),
  89.         create_point(p.x - 1, p.y),
  90.         create_point(p.x + 1, p.y)
  91.     };
  92.  
  93.     for (int i = 0; i < 4; i++) {
  94.         if (checkPoint(points + i) && L[points[i].x][points[i].y] == L[p.x][p.y] - 1) {
  95.             printSolution(L, points[i]);
  96.             printf("(%d, %d)\n", points[i].x +1, points[i].y + 1);
  97.             return;
  98.         }
  99.     }
  100. }
  101.  
  102. int main() {
  103.     FILE* file_input = fopen("Labirint.txt", "r");
  104.  
  105.     printf("Введите N и M: ");
  106.     scanf("%d %d", &N, &M);
  107.  
  108.     char l[N][M];
  109.  
  110.     char buffer[BUFFER_SIZE];
  111.     for (int i = 0; i < N; i++) {
  112.         fgets(buffer, BUFFER_SIZE, file_input);
  113.         for (int j = 0; j < M; j++)
  114.             l[i][j] = buffer[j];
  115.     }
  116.  
  117.     int L[N][M];
  118.     for (int i = 0; i < N; i++) {
  119.         for (int j = 0; j < M; j++) {
  120.             if (l[i][j] == '.') {
  121.                 L[i][j] = INT_MAX;
  122.             } else L[i][j] = -1;
  123.         }
  124.     }
  125.  
  126.     // Инициализация
  127.     Queue* q1 = create_queue();
  128.     Queue* q2 = create_queue();
  129.     L[0][0] = 0;
  130.     push(0, 0, q1);
  131.     int nc = 1;
  132.    
  133.  
  134.     // Итерация
  135.     while (!isEmpty(q1)) {
  136.        
  137.         while (!isEmpty(q1)) {
  138.             Point p = pop(q1);
  139.        
  140.             Point points[4] = {
  141.                 create_point(p.x, p.y - 1),
  142.                 create_point(p.x, p.y + 1),
  143.                 create_point(p.x - 1, p.y),
  144.                 create_point(p.x + 1, p.y)
  145.             };
  146.  
  147.             for (int i = 0; i < 4; i++) {
  148.                 int x = points[i].x;
  149.                 int y = points[i].y;
  150.                 if (checkPoint(points + i) && L[x][y] >= nc) {
  151.                     L[x][y] = nc;
  152.                     push(x, y, q2);
  153.                 }
  154.             }
  155.         }
  156.  
  157.         Queue *temp = q1;
  158.         q1 = q2;
  159.         q2 = temp;
  160.  
  161.         nc++;
  162.     }
  163.  
  164.     // Вывод результата
  165.     if (L[N-1][M-1] == INT_MAX) {
  166.         printf("no solution");
  167.     } else {
  168.         printf("%d\n", L[N-1][M-1]);
  169.         printSolution(L, create_point(N-1, M-1));
  170.         printf("(%d, %d)\n", N, M);
  171.     }
  172.    
  173.     return 0;
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement