88 lines
2.1 KiB
C
88 lines
2.1 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
typedef struct {
|
|
int x;
|
|
int y;
|
|
} Point;
|
|
|
|
int main() {
|
|
int N, M;
|
|
scanf("%d %d", &N, &M);
|
|
|
|
int** paper = (int**)malloc(sizeof(int*)*N);
|
|
int cheese = 0;
|
|
int time = 0;
|
|
|
|
for(int i=0; i<N; i++) {
|
|
paper[i] = (int*)malloc(sizeof(int)*M);
|
|
|
|
for(int j=0; j<M; j++) {
|
|
scanf("%d",&paper[i][j]);
|
|
|
|
if(paper[i][j] == 1) cheese++;
|
|
}
|
|
}
|
|
|
|
int** visited = (int**)malloc(sizeof(int*)*N);
|
|
for(int i=0; i<N; i++) visited[i] = (int*)malloc(sizeof(int)*M);
|
|
Point* qu = (Point*)malloc(sizeof(Point)*(N*M));
|
|
|
|
while(cheese != 0) {
|
|
for(int i=0; i<N; i++) memset(visited[i], 0, sizeof(int)*M);
|
|
|
|
int q_front = 0;
|
|
int q_rear = 0;
|
|
|
|
Point start = {0,0};
|
|
qu[q_rear++] = start;
|
|
visited[0][0] = 1;
|
|
|
|
int dx[4] = {-1,1,0,0};
|
|
int dy[4] = {0,0,1,-1};
|
|
|
|
while(q_front != q_rear) {
|
|
Point now = qu[q_front++];
|
|
|
|
for(int i=0; i<4; i++) {
|
|
int nx = now.x + dx[i];
|
|
int ny = now.y + dy[i];
|
|
|
|
if(0<=nx && nx<M && 0<=ny && ny<N && paper[ny][nx] == 0 && visited[ny][nx] == 0) {
|
|
Point next = {nx, ny};
|
|
qu[q_rear++] = next;
|
|
visited[ny][nx] = 1;
|
|
|
|
for(int j=0; j<4; j++) {
|
|
int nnx = next.x + dx[j];
|
|
int nny = next.y + dy[j];
|
|
|
|
if(0<=nnx && nnx<M && 0<=nny && nny<N && paper[nny][nnx] == 1) visited[nny][nnx]++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
for(int i=0; i<N; i++) {
|
|
for(int j=0; j<M; j++) {
|
|
if(paper[i][j] == 1 && visited[i][j] > 1) {
|
|
paper[i][j] = 0;
|
|
cheese--;
|
|
}
|
|
}
|
|
}
|
|
|
|
time++;
|
|
}
|
|
|
|
printf("%d\n",time);
|
|
|
|
for(int i=0; i<N; i++) free(paper[i]);
|
|
free(paper);
|
|
for(int i=0; i<N; i++) free(visited[i]);
|
|
free(visited);
|
|
free(qu);
|
|
|
|
return 0;
|
|
} |