Skip to content

Commit 7b8a86f

Browse files
authored
Merge pull request #685 from AlgorithmWithGod/suyeun84
[20250818] BOJ / G4 / 치즈 / 김수연
2 parents 808e3d9 + d2783b8 commit 7b8a86f

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
public class boj2636 {
6+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
static StringTokenizer st;
8+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
9+
static int nextInt() {return Integer.parseInt(st.nextToken());}
10+
11+
static int N, M, time = 0, answer = 0;
12+
static int[][] map;
13+
static int[][] dir = new int[][]{{1,0}, {-1,0}, {0,1}, {0,-1}};
14+
public static void main(String[] args) throws Exception {
15+
nextLine();
16+
N = nextInt();
17+
M = nextInt();
18+
map = new int[N][M];
19+
for (int i = 0; i < N; i++) {
20+
nextLine();
21+
for (int j = 0; j < M; j++) {
22+
map[i][j] = nextInt();
23+
if (map[i][j] == 1) answer++;
24+
}
25+
}
26+
while (answer > 0) {
27+
time++;
28+
int melted = solve();
29+
if (answer - melted == 0) break;
30+
answer -= melted;
31+
}
32+
System.out.println(time);
33+
System.out.println(answer);
34+
}
35+
// 밖이랑 연결되어 있는지 확인
36+
static int solve() {
37+
Queue<int[]> q = new LinkedList<>();
38+
boolean[][] visited = new boolean[N][M];
39+
q.add(new int[]{0, 0});
40+
int melted = 0;
41+
visited[0][0] = true;
42+
while(!q.isEmpty()) {
43+
int[] cur = q.poll();
44+
for (int[] d : dir) {
45+
int ny = cur[0] + d[0];
46+
int nx = cur[1] + d[1];
47+
if (ny < 0 || ny >= N || nx < 0 || nx >= M || visited[ny][nx]) continue;
48+
if (map[ny][nx] == 0) {
49+
visited[ny][nx] = true;
50+
q.add(new int[] {ny, nx});
51+
} else {
52+
map[ny][nx] = 0;
53+
visited[ny][nx] = true;
54+
melted++;
55+
}
56+
}
57+
}
58+
return melted;
59+
}
60+
}
61+
```

0 commit comments

Comments
 (0)