This repository was archived by the owner on Dec 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBOJ2667.java
More file actions
65 lines (54 loc) · 1.81 KB
/
Copy pathBOJ2667.java
File metadata and controls
65 lines (54 loc) · 1.81 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import java.util.Arrays;
import java.util.Scanner;
public class BOJ2667 {
static int[][] map;
static int count = 0;
static int index = 0;
static int[] sizeArray;
static boolean[][] visited;
static int[] xDir = new int[]{-1, 0, 1, 0};
static int[] yDir = new int[]{0, 1, 0, -1};
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
map = new int[n + 2][n + 2];
visited = new boolean[n + 1][n + 1];
sizeArray = new int[n * n];
Arrays.fill(map[0], -1);
for (int i = 1; i <= n; i++) {
map[i][0] = -1;
char[] strArray = sc.next().toCharArray();
for (int j = 1; j <= n; j++) {
map[i][j] = strArray[j - 1] - '0';
}
map[i][n + 1] = -1;
}
Arrays.fill(map[n + 1], -1);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (!visited[i][j] && map[i][j] == 1) {
dfs(i, j, 1);
index++;
}
}
}
System.out.println(index);
Arrays.sort(sizeArray);
for (int i = n * n - index; i < n * n; i++) {
System.out.println(sizeArray[i]);
}
}
private static void dfs(int x, int y, int size) {
if (size > sizeArray[index]) {
sizeArray[index] = size;
}
if (!visited[x][y]) {
visited[x][y] = true;
for (int i = 0; i < 4; i++) {
if (map[x + xDir[i]][y + yDir[i]] == 1 && !visited[x + xDir[i]][y + yDir[i]]) {
dfs(x + xDir[i], y + yDir[i], sizeArray[index] + 1);
}
}
}
}
}