Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <string.h>
- struct elem
- {
- int i;
- int j;
- };
- int isinfected(int **arr, int N, int M)
- {
- int k = 1, i = 0, j = 0;
- for (i = 1; i < N - 1; i++)
- for(j = 1; j < M - 1 ; j++)
- k = k * arr[i][j];
- return k;
- }
- int main (void)
- {
- int N, M, k, i, x, j = 0, time = 1;
- scanf("%d%d", &N, &M);
- int **colony = malloc(sizeof(int *) * (N + 2));
- for (i = 0; i < N + 2; i++)
- {
- colony[i] = malloc(sizeof(int) * (M + 2));
- }
- for (i = 1; i < N + 1; i++)
- for(j = 1; j < M + 1; j++)
- colony[i][j] = 0;
- scanf("%d", &k);
- struct elem *viruses = malloc(sizeof(struct elem) * k);
- for( x = 0; x < k; x++)
- {
- scanf("%d%d", &(viruses[x].j), &(viruses[x].i));
- }
- if (N * M == k)
- printf("%d", 0);
- else{
- for (i = 1; i < N + 1; i++)
- for(j = 1; j < M + 1; j++)
- for( x = 0; x < k; x++)
- if (i == viruses[x].i && j == viruses[x].j)
- colony[i][j] = 1;
- while (isinfected(colony, N + 2, M + 2) != 1)
- {
- for(i = 1; i < N + 1; i++)
- {
- if (isinfected(colony, N + 2, M + 2))
- {
- printf("%d", time);
- return 0;
- }
- for(j = 1; j < M + 1; j++)
- {
- if ( colony[i][j] == time)
- {
- if (colony [i][j] != colony[i - 1][j] )
- colony[i - 1][j] = 1 + colony[i][j];
- if (colony [i][j] != colony[i][j - 1] )
- colony[i][j - 1] = 1 + colony[i][j];
- if (colony [i][j] != colony[i][j + 1] )
- colony[i][j + 1] = 1 + colony[i][j];
- if (colony [i][j] != colony[i + 1][j] )
- colony[i + 1][j] = 1 + colony[i][j];
- }
- }
- }
- time++;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement