#️⃣ 문제
눈금의 간격이 MxN(M,N≤100)크기의 모눈종이가 있다. 이 모눈종이 위에 눈금을 맞추어 K개의 직사각형을 그릴 때, 이들 K개의 직사각형의 내부를 제외한 나머지 부분이 몇 개의 분리된 영역으로 나누어진다.
예를 들어 M=5, N=7 인 모눈종이 위에 <그림1>과 같이 직사각형 3개를 그렸다면, 그 나머지 영역은 <그림2>와 같이 3개의 분리된 영역으로 나누어지게 된다.
<그림2>와 같이 분리된 세 영역의 넓이는 각각 1, 7, 13이 된다.
M, N과 K 그리고 K개의 직사각형의 좌표가 주어질 때, K개의 직사각형 내부를 제외한 나머지 부분이 몇 개의 분리된 영역으로 나누어지는지, 그리고 분리된 각 영역의 넓이가 얼마인지를 구하여 이를 출력하는 프로그램을 작성하시오.
#️⃣ 입력
첫째 줄에 M과 N, 그리고 K가 빈칸을 사이에 두고 차례로 주어진다. M, N, K는 모두 100 이하의 자연수이다. 둘째 줄부터 K개의 줄에는 한 줄에 하나씩 직사각형의 왼쪽 아래 꼭짓점의 x, y좌표값과 오른쪽 위 꼭짓점의 x, y좌표값이 빈칸을 사이에 두고 차례로 주어진다. 모눈종이의 왼쪽 아래 꼭짓점의 좌표는 (0,0)이고, 오른쪽 위 꼭짓점의 좌표는(N,M)이다. 입력되는 K개의 직사각형들이 모눈종이 전체를 채우는 경우는 없다.
#️⃣ 출력
첫째 줄에 분리되어 나누어지는 영역의 개수를 출력한다. 둘째 줄에는 각 영역의 넓이를 오름차순으로 정렬하여 빈칸을 사이에 두고 출력한다.
✅ 풀이 Point
1️⃣ bfs() 메서드에 들어갈 때마다 하나의 인접한 영역은 다 방문 처리가 되게 만듬.
‣ main() 메서드에서 bfs() 메서드로 진입할 때는 영역의 개수 증가
int cnt = 0;
ArrayList<Integer> cntArea = new ArrayList<>();
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (arr[i][j] == 0 && !visited[i][j]) {
cnt++;
cntArea.add(bfs(i, j));
}
}
}
‣ bfs() 메서드 내에서 queue에 요소 추가시 영역의 넓이 증가
static int bfs(int ci, int cj) {
Queue<Node> q = new LinkedList<Node>();
q.offer(new Node(ci, cj));
visited[ci][cj] = true;
int cnt = 1;
while (!q.isEmpty()) {
Node node = q.poll();
for (int k = 0; k < 4; k++) {
int ni = node.ci + di[k];
int nj = node.cj + dj[k];
if (0 <= ni && ni < N && 0 <= nj && nj < M && !visited[ni][nj] && arr[ni][nj] == 0) {
cnt++;
visited[ni][nj] = true;
q.offer(new Node(ni, nj));
}
}
}
return cnt;
}
✅ 정답
import java.io.*;
import java.util.*;
public class Main {
static int N, M, K;
static int[][] arr;
static boolean[][] visited;
static int[] di = {-1, 0, 1, 0};
static int[] dj = {0, 1, 0, -1};
static class Node {
int ci;
int cj;
public Node(int ci, int cj) {
this.ci = ci;
this.cj = cj;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());
arr = new int[N][M];
visited = new boolean[N][M];
for (int t = 0; t < K; t++) {
st = new StringTokenizer(br.readLine());
int sj = Integer.parseInt(st.nextToken());
int si = Integer.parseInt(st.nextToken());
int ej = Integer.parseInt(st.nextToken());
int ei = Integer.parseInt(st.nextToken());
for (int i = si; i < ei; i++) {
for (int j = sj; j < ej; j++) {
arr[i][j] = 1;
}
}
}
int cnt = 0;
ArrayList<Integer> cntArea = new ArrayList<>();
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (arr[i][j] == 0 && !visited[i][j]) {
cnt++;
cntArea.add(bfs(i, j));
}
}
}
Collections.sort(cntArea);
System.out.println(cnt);
for (Integer area : cntArea) {
System.out.print(area + " ");
}
}
static int bfs(int ci, int cj) {
Queue<Node> q = new LinkedList<Node>();
q.offer(new Node(ci, cj));
visited[ci][cj] = true;
int cnt = 1;
while (!q.isEmpty()) {
Node node = q.poll();
for (int k = 0; k < 4; k++) {
int ni = node.ci + di[k];
int nj = node.cj + dj[k];
if (0 <= ni && ni < N && 0 <= nj && nj < M && !visited[ni][nj] && arr[ni][nj] == 0) {
cnt++;
visited[ni][nj] = true;
q.offer(new Node(ni, nj));
}
}
}
return cnt;
}
}
반응형
'Algorithm > Java' 카테고리의 다른 글
[백준 : 실버 4] Java BJ2578 빙고 (0) | 2024.02.07 |
---|---|
[백준 : 실버 1] Java BJ1149 RGB 거리 (0) | 2024.02.06 |
[백준 : 실버 2] Java BJ4963 섬의 개수 (0) | 2024.01.25 |
[백준 : 실버 4] Java BJ11399 ATM (0) | 2024.01.24 |
[백준 : 실버 4] Java BJ17266 어두운 굴다리 (0) | 2024.01.22 |