Advertisement
Refreezer

Untitled

Dec 12th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.04 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <string.h>
  5. struct elem
  6. {
  7.     int i;
  8.     int j;
  9. };
  10.  
  11. int isinfected(int **arr, int N, int M)
  12. {
  13.     int k = 1, i = 0, j = 0;
  14.     for (i = 1; i < N - 1; i++)
  15.         for(j = 1; j < M - 1 ; j++)
  16.             k = k * arr[i][j];
  17.     return k;
  18. }
  19. int main (void)
  20. {
  21.     int N, M, k, i, x, j = 0, time = 1;
  22.     scanf("%d%d", &N, &M);
  23.     int **colony = malloc(sizeof(int *) * (N + 2));
  24.     for (i = 0; i < N + 2; i++)
  25.     {
  26.         colony[i] = malloc(sizeof(int) * (M + 2));
  27.     }
  28.     for (i = 1; i < N + 1; i++)
  29.         for(j = 1; j < M + 1; j++)
  30.             colony[i][j] = 0;
  31.     scanf("%d", &k);
  32.     struct elem *viruses = malloc(sizeof(struct elem) * k);
  33.     for( x = 0; x < k; x++)
  34.     {
  35.         scanf("%d%d", &(viruses[x].j), &(viruses[x].i));
  36.     }
  37.     if (N * M == k)
  38.         printf("%d", 0);
  39.     else{
  40.     for (i = 1; i < N + 1; i++)
  41.         for(j = 1; j < M + 1; j++)
  42.             for( x = 0; x < k; x++)
  43.                 if (i == viruses[x].i  && j == viruses[x].j)
  44.                     colony[i][j] = 1;
  45.     while (isinfected(colony, N + 2, M + 2) != 1)
  46.     {
  47.         for(i = 1; i < N + 1; i++)
  48.         {
  49.             if (isinfected(colony, N + 2, M + 2))
  50.             {
  51.                 printf("%d", time);
  52.                 return 0;
  53.             }
  54.             for(j = 1; j < M + 1; j++)
  55.             {
  56.                 if ( colony[i][j] == time)
  57.                 {
  58.                     if (colony [i][j] != colony[i - 1][j] )
  59.                          colony[i - 1][j] = 1 + colony[i][j];
  60.                     if (colony [i][j] != colony[i][j - 1] )
  61.                          colony[i][j - 1] = 1 + colony[i][j];
  62.                     if (colony [i][j] != colony[i][j + 1] )
  63.                          colony[i][j + 1] = 1 + colony[i][j];
  64.                     if (colony [i][j] != colony[i + 1][j] )
  65.                         colony[i + 1][j] = 1 + colony[i][j];
  66.  
  67.  
  68.                 }
  69.  
  70.             }
  71.  
  72.  
  73.  
  74.         }
  75.         time++;
  76.     }
  77.     }
  78.  
  79.     return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement