111 lines
2.5 KiB
C
111 lines
2.5 KiB
C
#include <stdio.h>
|
|
#include <stdbool.h>
|
|
|
|
int map[1000][1000];
|
|
int group[1000][1000];
|
|
int group_size[1000*1000];
|
|
int N, M;
|
|
bool visited[1000][1000];
|
|
|
|
int group_num;
|
|
int qu[1000*1000][2];
|
|
int front, rear;
|
|
int dx[4] = {-1, 1, 0, 0};
|
|
int dy[4] = {0, 0, 1, -1};
|
|
|
|
int result[1000][1000];
|
|
|
|
bool isPossible(int x, int y) {
|
|
return (0<=x && x<M) && (0<=y && y<N) && map[y][x] == 0 && !visited[y][x];
|
|
}
|
|
|
|
void BFS(int x, int y) {
|
|
front = 0, rear = 0;
|
|
qu[rear][0] = x;
|
|
qu[rear++][1] = y;
|
|
visited[y][x] = true;
|
|
group[y][x] = group_num;
|
|
int cnt = 1;
|
|
|
|
while(front < rear) {
|
|
int cx = qu[front][0];
|
|
int cy = qu[front++][1];
|
|
|
|
for(int i=0; i<4; i++) {
|
|
int ny = cy + dy[i];
|
|
int nx = cx + dx[i];
|
|
|
|
if(isPossible(nx, ny)) {
|
|
qu[rear][0] = nx;
|
|
qu[rear++][1] = ny;
|
|
visited[ny][nx] = true;
|
|
group[ny][nx] = group_num;
|
|
cnt++;
|
|
}
|
|
}
|
|
}
|
|
|
|
group_size[group_num] = cnt;
|
|
}
|
|
|
|
int main() {
|
|
scanf("%d %d",&N, &M);
|
|
for(int i=0; i<N; i++) {
|
|
char line[10001];
|
|
scanf("%s", line);
|
|
for(int j=0; j<M; j++) {
|
|
map[i][j] = line[j] - '0';
|
|
}
|
|
}
|
|
|
|
for(int i=0; i<N; i++) {
|
|
for(int j=0; j<M; j++) {
|
|
if(map[i][j] == 0 && !visited[i][j]) {
|
|
BFS(j, i);
|
|
group_num++;
|
|
}
|
|
}
|
|
}
|
|
|
|
for(int y=0; y<N; y++) {
|
|
for(int x=0; x<M; x++) {
|
|
if(map[y][x] != 1) continue;
|
|
|
|
int cnt = 1;
|
|
int nearby_groups[4];
|
|
int nearby_idx = 0;
|
|
|
|
for(int i=0; i<4; i++) {
|
|
int nx = x + dx[i];
|
|
int ny = y + dy[i];
|
|
|
|
if(0<=nx && nx < M && 0<=ny && ny < N && map[ny][nx] == 0) {
|
|
int current_group = group[ny][nx];
|
|
|
|
bool group_checking = true;
|
|
for(int i=0; i<nearby_idx; i++) {
|
|
if(nearby_groups[i] == current_group) {
|
|
group_checking = false;
|
|
}
|
|
}
|
|
|
|
if(group_checking) {
|
|
nearby_groups[nearby_idx++] = current_group;
|
|
cnt += group_size[current_group];
|
|
}
|
|
}
|
|
}
|
|
|
|
result[y][x] = cnt%10;
|
|
}
|
|
}
|
|
|
|
for(int i=0; i<N; i++) {
|
|
for(int j=0; j<M; j++) {
|
|
printf("%d",result[i][j]);
|
|
}
|
|
printf("\n");
|
|
}
|
|
|
|
return 0;
|
|
} |