[99클럽 코테 스터디 18일차 TIL] 백준 - 단지번호붙이기
99클럽 코테 스터디 18일차 TIL 입니다.
Last updated
99클럽 코테 스터디 18일차 TIL 입니다.
Last updated
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
public class Main {
static int n;
static int[][] map; // 지도
static boolean[][] visited; // 방문 유무
static int[] dx = {0, 1, 0, -1}; // 좌하우상
static int[] dy = {-1, 0, 1, 0}; // 좌하우상
static int apt = 0; // 단지 수
static int[] apts; // 단지 최대 개수
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
n = Integer.parseInt(br.readLine());
map = new int[n][n];
visited = new boolean[n][n];
apts = new int[n*n];
// 지도 받기
for (int i = 0; i < n; i++) {
String[] p = br.readLine().split("");
for (int j = 0; j < n; j++) {
map[i][j] = Integer.parseInt(p[j]);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (map[i][j] == 1 && !visited[i][j]) {
apt++;
bfs(i, j);
}
}
}
Arrays.sort(apts);
sb.append(apt).append("\n");
for (int a : apts) {
if (a != 0) {
sb.append(a).append("\n");
}
}
System.out.println(sb);
}
static void bfs(int x, int y) {
Queue<int[]> queue = new LinkedList<>();
queue.add(new int[]{x, y});
visited[x][y] = true;
apts[apt]++;
while (!queue.isEmpty()) {
int[] cur = queue.poll();
int currX = cur[0];
int currY = cur[1];
for (int i = 0; i < 4; i++) {
int nx = currX + dx[i];
int ny = currY + dy[i];
if (nx < 0 || nx >= n || ny < 0 || ny >= n) continue;
if (map[nx][ny] != 1 || visited[nx][ny]) continue;
visited[nx][ny] = true;
queue.add(new int[]{nx, ny});
apts[apt]++;
}
}
}
}import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
static int n;
static int[][] map; // 지도
static int[][] dxy = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; // 상하좌우
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
n = Integer.parseInt(br.readLine());
map = new int[n][n];
List<Integer> complex = new ArrayList<>();
// 지도 받기
for (int i = 0; i < n; i++) {
String[] p = br.readLine().split("");
for (int j = 0; j < n; j++) {
map[i][j] = Integer.parseInt(p[j]);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (map[i][j] == 1) {
complex.add(bfs(i, j));
}
}
}
sb.append(complex.size()).append("\n");
complex.stream().sorted().forEach(i -> sb.append(i).append("\n"));
System.out.println(sb);
}
static int bfs(int x, int y) {
Queue<int[]> queue = new LinkedList<>();
queue.add(new int[]{x, y});
map[x][y] = 0;
int cnt = 1;
while (!queue.isEmpty()) {
int[] cur = queue.poll();
int currX = cur[0];
int currY = cur[1];
for (int i = 0; i < 4; i++) {
int nx = currX + dxy[i][0];
int ny = currY + dxy[i][1];
if (nx < 0 || nx >= n || ny < 0 || ny >= n) continue;
if (map[nx][ny] != 1) continue;
cnt++;
queue.add(new int[]{nx, ny});
map[nx][ny] = 0;
}
}
return cnt;
}
}