728x90
상하좌우가 인접한 경우 하나의 단지로 생각합니다.
몇개의 단지가 있는지와 단지안에 집들의 수를 구하는 문제입니다.
DFS, BFS를 사용해서 단지안에 집들을 카운트해줍니다.
단지들을 출력하고 단지안의 집들을 정렬해서 출력해주었습니다.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
Queue<Integer> qy = new LinkedList<>();
Queue<Integer> qx = new LinkedList<>();
int n = Integer.parseInt(br.readLine());
int[][] map = new int[n][n];
int[][] visit = new int[n][n];
int[] dy = {0, 1, 0, -1};
int[] dx = {1, 0, -1, 0};
int[] ans = new int[n*n];
int index = 0;
for(int i=0; i<n; ++i) {
String str = br.readLine();
for(int j=0; j<n; ++j) {
map[i][j] = str.charAt(j) == '0' ? 0 : 1;
}
}
for(int i=0; i<n; ++i) {
for(int j=0; j<n; ++j) {
if(visit[i][j] == 1 || map[i][j] == 0) {
continue;
}
qy.add(i);
qx.add(j);
visit[i][j] = 1;
int num = 0;
while(!qy.isEmpty()) {
int y = qy.poll();
int x = qx.poll();
++num;
for(int k=0; k<4; ++k) {
int nextY = y + dy[k];
int nextX = x + dx[k];
if(nextY < 0 || nextX < 0 || nextY >= n || nextX >= n) {
continue;
}
if(visit[nextY][nextX] == 1 || map[nextY][nextX] == 0) {
continue;
}
qy.add(nextY);
qx.add(nextX);
visit[nextY][nextX] = 1;
}
}
ans[index] = num;
++index;
}
}
Arrays.sort(ans);
bw.write(index + "\n");
for(int i=0; i<index; ++i) {
bw.write(ans[n*n-index+i] + "\n");
}
bw.close();
}
}
'알고리즘' 카테고리의 다른 글
[Java] 백준 2644번 촌수계산 (0) | 2023.02.23 |
---|---|
[Java] 백준 1697번 숨바꼭질 (0) | 2023.02.23 |
[Java] 백준 4963번 섬의 개수 (0) | 2023.02.23 |
[Java] 백준 1654번 랜선 자르기 (0) | 2023.02.23 |
[JavaScript] 프로그래머스 - 등산코스 정하기 (0) | 2023.02.22 |