baekjoon 20260112
This commit is contained in:
parent
ca4a7fdb2c
commit
a0b2080c9c
68
code_study/Baekjoon/c/16724.c
Normal file
68
code_study/Baekjoon/c/16724.c
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
char field[1001][1001];
|
||||||
|
int N, M;
|
||||||
|
int parent[1000000];
|
||||||
|
int rank[1000000];
|
||||||
|
|
||||||
|
int find(int x) {
|
||||||
|
if(x != parent[x]) return parent[x] = find(parent[x]);
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
void swap(int *a, int *b) {
|
||||||
|
int temp = *a;
|
||||||
|
*a = *b;
|
||||||
|
*b = temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Union(int x, int y) {
|
||||||
|
x = find(x);
|
||||||
|
y = find(y);
|
||||||
|
|
||||||
|
if(rank[x] < rank[y]) swap(&x, &y);
|
||||||
|
|
||||||
|
parent[y] = x;
|
||||||
|
|
||||||
|
if(rank[x] == rank[y]) rank[x]++;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int area_num(int i, int j) {
|
||||||
|
return M*i + j;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
scanf("%d %d",&N, &M);
|
||||||
|
for(int i=0; i<N; i++) {
|
||||||
|
scanf("%s",field[i]);
|
||||||
|
for(int j=0; j<M; j++) {
|
||||||
|
int area = area_num(i, j);
|
||||||
|
parent[area] = area;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int i=0; i<N; i++) {
|
||||||
|
for(int j=0; j<M; j++) {
|
||||||
|
char arrow = field[i][j];
|
||||||
|
int now = area_num(i, j);
|
||||||
|
int next;
|
||||||
|
|
||||||
|
if(arrow == 'D') next = area_num(i+1, j);
|
||||||
|
else if(arrow == 'U') next = area_num(i-1,j);
|
||||||
|
else if(arrow == 'L') next = area_num(i,j-1);
|
||||||
|
else next = area_num(i,j+1);
|
||||||
|
|
||||||
|
Union(now, next);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int ans = 0;
|
||||||
|
for(int i=0; i<M*N; i++) {
|
||||||
|
if(i == parent[i]) ans++;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("%d\n",ans);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
111
code_study/Baekjoon/c/16946.c
Normal file
111
code_study/Baekjoon/c/16946.c
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
#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;
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user