-
Notifications
You must be signed in to change notification settings - Fork 6
/
HyeKyoung.java
60 lines (51 loc) · 1.45 KB
/
HyeKyoung.java
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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class HyeKyoung {
static boolean[][] isVisited;
static int[][] map;
static int[] dr = { -1, 1, 0, 0 };
static int[] dc = { 0, 0, -1, 1 };
static int n, cnt = 0;
static List<Integer> list = new ArrayList<>();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
map = new int[n][n];
isVisited = new boolean[n][n];
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';
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
if (map[i][j] == 0) {
isVisited[i][j] = true;
continue;
}
if (!isVisited[i][j]) {
apartmentCnt(i, j);
list.add(cnt); // 다음 위치로 넘긴 좌표가 모두 방문했던가 0
cnt = 0;
}
}
System.out.println(list.size());
Collections.sort(list);
for (int x : list) System.out.println(x);
}
static void apartmentCnt(int r, int c) {
cnt++;
isVisited[r][c] = true;
for (int i = 0; i < 4; i++) {
int nr = r + dr[i];
int nc = c + dc[i];
if (nr < 0 || nr >= n || nc < 0 || nc >= n) continue;
if (isVisited[nr][nc] || map[nr][nc] == 0) continue;
else apartmentCnt(nr, nc);
}
}
}