Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- KB국민은행
- 이차원배열
- autohotkey
- SQL데이터타입
- 부트캠프
- 빅데이터분석기사
- 반별이벤트
- 첫알고리즘평가
- kb 취업교육
- kb it's your life
- 전문가특강
- 데이터분석자격증
- 멀티캠퍼스
- 빅분기
- 빅분기필기
- 빅데이터분석기사필기
- kb네트워킹캠프
- 데이터분석
- 금융권it
- kb it's your life 6기
- kb 기자단
- SQLD
- prefixsum #C언어
- 금융권 부트캠프
- sql
- kb취업교육
- 금융권 it
- sql내장함수
- kb it's your life 기자단
- kbit교육
Archives
- Today
- Total
지식보부상님의 공부 일지
백준 2668번: 단지 번호 붙이기 (bfs이용+sorting) 본문
https://www.acmicpc.net/problem/2667
2667번: 단지번호붙이기
<그림 1>과 같이 정사각형 모양의 지도가 있다. 1은 집이 있는 곳을, 0은 집이 없는 곳을 나타낸다. 철수는 이 지도를 가지고 연결된 집의 모임인 단지를 정의하고, 단지에 번호를 붙이려 한다. 여
www.acmicpc.net
#include <stdio.h>
int graph[26][26] = { 0, };
int n = 0;
int queue[1000][2] = { 0, };
int cnt = 0;
int town[1000] = { 0, };
void bfs(int v, int w) {
int front = 0, rear = 0;
int ans = 1;
int dx[4] = { -1, 1, 0, 0 };
int dy[4] = { 0, 0, -1, 1 };
queue[rear][0] = v;
queue[rear++][1] = w;
graph[v][w] = 0;
while (front < rear) {
int x = queue[front][0];
int y = queue[front++][1];
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx<1 || ny<1 || nx>n || ny>n)
continue;
if (graph[nx][ny] != 1)
continue;
graph[nx][ny] = 0;
queue[rear][0] = nx;
queue[rear++][1] = ny;
ans++;
}
}
town[cnt++] = ans;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
scanf("%1d", &graph[i][j]);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (graph[i][j] == 1)
bfs(i, j);
printf("%d\n", cnt);
for (int i = 0; i < cnt; i++) {
for (int j = i+1; j < cnt; j++) {
if (town[i] > town[j]) {
int tmp = town[i];
town[i] = town[j];
town[j] = tmp;
}
}
}
for (int i = 0; i < cnt; i++)
printf("%d\n", town[i]);
return 0;
}
'C언어 문제풀이' 카테고리의 다른 글
SWEA 1249번: [S/W 문제해결 응용] 4일차 - 보급로 (heap 이용) (0) | 2024.11.14 |
---|---|
에러 함수 (0) | 2024.11.14 |
백준 2606번: 바이러스 (dfs 이용) (0) | 2024.01.12 |
백준 2606번: 바이러스 (bfs 이용) (0) | 2024.01.12 |
백준 2178번: 미로탐색 (bfs 이용) (0) | 2024.01.12 |