artmexbet

finaly_hueta

Apr 4th, 2024
3,791
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.54 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <pthread.h>
  5. #include <string.h>
  6.  
  7. #define ROWS 4
  8. #define COLS 5
  9.  
  10. struct ThreadResult {
  11.     int count;
  12.     int indexes[6];
  13. };
  14.  
  15. struct ThreadInfo {
  16.     int matrix[ROWS][COLS];
  17.     int deltaX;
  18.     int deltaY;
  19.     int currentIndexI;
  20.     int currentIndexJ;
  21.     struct ThreadResult result;
  22. };
  23.  
  24. void fill_matrix(int array[ROWS][COLS]) {
  25.     srand(time(NULL));
  26.     for (int i = 0; i < ROWS; i++) {
  27.         for (int j = 0; j < COLS; j++) {
  28.             array[i][j] = 1 + rand() % 3;
  29.         }
  30.     }
  31. }
  32.  
  33. void print_matrix(int array[ROWS][COLS]) {
  34.     for (int i = 0; i < ROWS; i++) {
  35.         for (int j = 0; j < COLS; j++) {
  36.             printf("%d ", array[i][j]);
  37.         }
  38.         printf("\n");
  39.     }
  40. }
  41.  
  42. void *get_values(void *info_) {
  43.     struct ThreadInfo *info = (struct ThreadInfo *) info_;
  44.     struct ThreadResult *result = (struct ThreadResult *) malloc(sizeof(struct ThreadResult));
  45.     result->count = 0;
  46.     // Вот отсюда можно выкинуть вычисление уголочка из этой точки в отдельный поток
  47.     int k = 0;
  48.     int lengthOfHorizontal = 0; // Сюда сохраняем длину повторящихся символов в строке
  49.     int i = info->currentIndexI;
  50.     int j = info->currentIndexJ;
  51.     while (0 < j + k && j + k < COLS && info->matrix[i][j + k] == info->matrix[i][j]) {
  52.         lengthOfHorizontal++;
  53.         // Или вот отсюда можно запускать потоки, проверяющие вертикальные линии на совпадение с элементом matrix[i][j]
  54.         // Нижний цикл выкинуть в отдельный поток
  55.         int l = 0;
  56.         int lengthOfVertical = 0; // Сюда сохраняем длину повторяющихся символов в столбце
  57.         while (0 < i + l && i + l < ROWS && info->matrix[i + l][j + k] == info->matrix[i][j + k]) {
  58.             lengthOfVertical++;
  59.             l += info->deltaX;
  60.         }
  61.         if (lengthOfHorizontal + lengthOfVertical > result->count) {
  62.             result->indexes[0] = i;
  63.             result->indexes[1] = j;
  64.             result->indexes[2] = i;
  65.             result->indexes[3] = j + k;
  66.             result->indexes[4] = i + l - 1 * info->deltaX;
  67.             result->indexes[5] = j + k;
  68.             result->count = lengthOfHorizontal + lengthOfVertical;
  69. //                    coords = {i, j, i, j + k, i + l - 1, j + k};
  70.         }
  71.         k += info->deltaY;
  72.     }
  73.     return (void *) result;
  74. }
  75.  
  76. void find_g(int matrix[ROWS][COLS]) {
  77.     int maxCount = 0;
  78.     int coords[] = {0, 0, 0, 0, 0, 0};
  79.     for (int i = 0; i < ROWS; i++) {
  80.         for (int j = 0; j < COLS; j++) {
  81.             struct ThreadInfo first, second, third, fourth;
  82.             struct ThreadResult *result1, *result2, *result3, *result4;
  83.             memcpy(first.matrix, matrix, ROWS * COLS * sizeof(int));
  84.             memcpy(second.matrix, matrix, ROWS * COLS * sizeof(int));
  85.             memcpy(third.matrix, matrix, ROWS * COLS * sizeof(int));
  86.             memcpy(fourth.matrix, matrix, ROWS * COLS * sizeof(int));
  87.             first.deltaX = 1;
  88.             first.deltaY = -1;
  89.             first.currentIndexI = i;
  90.             first.currentIndexJ = j;
  91.  
  92.             second.deltaX = -1;
  93.             second.deltaY = -1;
  94.             second.currentIndexI = i;
  95.             second.currentIndexJ = j;
  96.  
  97.             third.deltaX = -1;
  98.             third.deltaY = 1;
  99.             third.currentIndexI = i;
  100.             third.currentIndexJ = j;
  101.  
  102.             fourth.deltaX = 1;
  103.             fourth.deltaY = 1;
  104.             fourth.currentIndexI = i;
  105.             fourth.currentIndexJ = j;
  106.  
  107.             pthread_t thread1, thread2, thread3, thread4;
  108.             pthread_create(&thread1, NULL, &get_values, &first);
  109.             pthread_create(&thread2, NULL, &get_values, &second);
  110.             pthread_create(&thread3, NULL, &get_values, &third);
  111.             pthread_create(&thread4, NULL, &get_values, &fourth);
  112.             pthread_join(thread1, (void **) &result1);
  113.             pthread_join(thread2, (void **) &result2);
  114.             pthread_join(thread3, (void **) &result3);
  115.             pthread_join(thread4, (void **) &result4);
  116. //            for (int i = 0; i < 6; i++) {
  117. //                printf("%d ", first.result.indexes[i]);
  118. //            }
  119. //            printf("\n");
  120.             if (result1->count > maxCount) {
  121.                 maxCount = result1->count;
  122.                 memcpy(coords, result1->indexes, sizeof(result1->indexes));
  123.             }
  124.             if (result2->count > maxCount) {
  125.                 maxCount = result2->count;
  126.                 memcpy(coords, result2->indexes, sizeof(result2->indexes));
  127.             }
  128.             if (result3->count > maxCount) {
  129.                 maxCount = result3->count;
  130.                 memcpy(coords, result3->indexes, sizeof(result3->indexes));
  131.             }
  132.             if (result4->count > maxCount) {
  133.                 maxCount = result4->count;
  134.                 memcpy(coords, result4->indexes, sizeof(result4->indexes));
  135.             }
  136.         }
  137.     }
  138.     for (int i = 0; i < 6; i++) {
  139.         printf("%d ", coords[i]);
  140.     }
  141. }
  142.  
  143. int main() {
  144. //    printf("Hello, World!\n");
  145.     int arr[ROWS][COLS] = {{0, 2, 0, 2, 0},
  146.                            {0, 1, 1, 1, 0},
  147.                            {2, 0, 2, 1, 1},
  148.                            {0, 2, 0, 1, 1}};
  149. //    fill_matrix(arr);
  150.     print_matrix(arr);
  151.     find_g(arr);
  152.     return 0;
  153. }
  154.  
Advertisement
Add Comment
Please, Sign In to add comment