[PS] 99ν΄λ½ μ½ν μ€ν°λ 18μΌμ°¨ TIL (λ¨μ§λ²νΈλΆμ΄κΈ°)
νκ·Έ: 99ν΄λ½, BFS/DFS, PS, TIL, μ½λ©ν μ€νΈμ€λΉ, νν΄99
μΉ΄ν κ³ λ¦¬: PS
λ¬Έμ
μ€λͺ
μ΄λ² λ¬Έμ λ κ·Έλνμ λν μ λ³΄λ§ μ£Όμ΄μ§κ³ κ·Έ κ·Έλνλ₯Ό νμνλ κ²μ΄ μλλΌ μ£Όμ΄μ§ μ
λ ₯κ°μμ κ·Έλν νμμ΄ μ΄λ£¨μ΄μ ΈμΌ ν λΆλΆμ μ°ΎμμΌ νλ€.
μλ κ·Έλ¦Όμ²λΌ 맨 μ²μ νλͺ©λΆν° 1
μ΄ μ ν λΆλΆμ νμν΄μΌ νλ€.
1
λΆλΆμ λ§λλ©΄ μ΄λν μ μλ κ²½λ‘λ μ,ν,μ’,μ° μμμ λν΄ λ€μ νμμ μννλ€. μ΄λ dx dy λ°©μμΌλ‘ λ°©ν₯μ λν μμΉλ₯Ό λ°°μ΄μ 미리 μ μ₯ν΄ λκ³ μ¬μ©νλ©΄ μ½λκ° κ°κ²°ν΄μ§λ€.
1
2
3
4
5
6
7
8
static int[][] pos = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; // μνμ’μ°
...
// μνμ’μ° νμ
for (int i = 0; i < pos.length; i++){
// λ°©ν₯ μ΄λ ν μ’ν
int nx = x + pos[i][0];
int ny = y + pos[i][1];
}
dfs
ν¨μ λ΄μμ μ,ν,μ’,μ° μ’νλ‘ κ³μ κ°±μ νλ©΄μ κ°κ°μ μμμμ λ€μ dfs
νλ€. μ΄λ μ΄λ νμλμ μ’ν μΈλ±μ€κ° graph
λ°°μ΄μ μΈλ±μ€λ₯Ό λ²μ΄λλ κ²½μ°λ₯Ό λ°©μ§νκΈ° μν΄ λ²μ λ΄μ μμ κ²½μ°μλ§ νμνλλ‘ νλ€.
1
2
3
4
// μ΄λν κ²½λ‘κ° graph λ°°μ΄ λ²μ μμ μλμ§ κ²μ¬
private static boolean isRange(int nx, int ny) {
return nx >= 0 && nx < N && ny >= 0 && ny < N;
}
νμ΄ (DFS)
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
public class Main {
static int[][] graph;
static boolean[][] visited;
static int N;
static int[][] pos = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; // μνμ’μ°
static int count;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
graph = new int[N][N];
visited = new boolean[N][N];
for (int i = 0; i < N; i++) {
String s = br.readLine();
for (int j = 0; j < N; j++) {
graph[i][j] = s.charAt(j) - '0';
}
}
ArrayList<Integer> result = new ArrayList<>();
// (0,0) λΆν° λͺ¨λ μμμ λν΄ dfs
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (!visited[i][j] && graph[i][j] == 1) {
count = 0;
dfs(i, j);
result.add(count);
}
}
}
System.out.println(result.size());
Collections.sort(result);
for (Integer integer : result) {
System.out.println(integer);
}
}
private static void dfs(int x, int y) {
visited[x][y] = true;
count++;
// μνμ’μ° νμ
for (int i = 0; i < pos.length; i++) {
int nx = x + pos[i][0];
int ny = y + pos[i][1];
if (isRange(nx, ny) && !visited[nx][ny] && graph[nx][ny] == 1) {
dfs(nx, ny);
}
}
}
private static boolean isRange(int nx, int ny) {
return nx >= 0 && nx < N && ny >= 0 && ny < N;
}
}
νμ΄ (BFS)
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
66
67
68
public class Main {
static int[][] graph;
static boolean[][] visited;
static int N;
static int[][] pos = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; // μνμ’μ°
static int count;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
graph = new int[N][N];
visited = new boolean[N][N];
for (int i = 0; i < N; i++) {
String s = br.readLine();
for (int j = 0; j < N; j++) {
graph[i][j] = s.charAt(j) - '0';
}
}
ArrayList<Integer> result = new ArrayList<>();
// (0,0) λΆν° λͺ¨λ μμμ λν΄ dfs
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (!visited[i][j] && graph[i][j] == 1) {
count = 1;
bfs(i, j);
result.add(count);
}
}
}
System.out.println(result.size());
Collections.sort(result);
for (Integer integer : result) {
System.out.println(integer);
}
}
private static void bfs(int x, int y) {
Queue<int[]> queue = new LinkedList<>();
queue.offer(new int[] {x, y});
visited[x][y] = true;
while (!queue.isEmpty()) {
int[] current = queue.poll();
int cx = current[0];
int cy = current[1];
// μνμ’μ° νμ
for (int i = 0; i < pos.length; i++) {
int nx = cx + pos[i][0];
int ny = cy + pos[i][1];
if (isRange(nx, ny) && !visited[nx][ny] && graph[nx][ny] == 1) {
visited[nx][ny] = true;
queue.offer(new int[] {nx, ny});
count++;
}
}
}
}
private static boolean isRange(int nx, int ny) {
return nx >= 0 && nx < N && ny >= 0 && ny < N;
}
}
λκΈλ¨κΈ°κΈ°