From d531e6c9a0b5bdf53b99d6265395a23f633fb9f7 Mon Sep 17 00:00:00 2001 From: JHLEE325 <82587652+JHLEE325@users.noreply.github.com> Date: Sun, 9 Nov 2025 23:41:10 +0900 Subject: [PATCH] =?UTF-8?q?[20251109]=20BOJ=20/=20G3=20/=20=EC=B9=98?= =?UTF-8?q?=EC=A6=88=20/=20=EC=9D=B4=EC=A4=80=ED=9D=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../09 BOJ G3 \354\271\230\354\246\210.md" | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 "JHLEE325/202511/09 BOJ G3 \354\271\230\354\246\210.md" diff --git "a/JHLEE325/202511/09 BOJ G3 \354\271\230\354\246\210.md" "b/JHLEE325/202511/09 BOJ G3 \354\271\230\354\246\210.md" new file mode 100644 index 00000000..5ba2de3a --- /dev/null +++ "b/JHLEE325/202511/09 BOJ G3 \354\271\230\354\246\210.md" @@ -0,0 +1,89 @@ +```java +import java.io.*; +import java.util.*; + +public class Main { + static int N, M; + static int[][] map; + static boolean[][] external; + static int[] dy = { -1, 0, 1, 0 }; + static int[] dx = { 0, 1, 0, -1 }; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + + map = new int[N][M]; + int cheeseCount = 0; + for (int i = 0; i < N; i++) { + st = new StringTokenizer(br.readLine()); + for (int j = 0; j < M; j++) { + map[i][j] = Integer.parseInt(st.nextToken()); + if (map[i][j] == 1) cheeseCount++; + } + } + + int time = 0; + while (cheeseCount > 0) { + external = new boolean[N][M]; + markExternalAir(); + + List meltList = new ArrayList<>(); + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + if (map[i][j] == 1) { + int contact = 0; + for (int d = 0; d < 4; d++) { + int ny = i + dy[d]; + int nx = j + dx[d]; + if (ny < 0 || nx < 0 || ny >= N || nx >= M) continue; + if (external[ny][nx] && map[ny][nx] == 0) { + contact++; + } + } + if (contact >= 2) { + meltList.add(new int[]{i, j}); + } + } + } + } + for (int[] pos : meltList) { + int y = pos[0], x = pos[1]; + map[y][x] = 0; + cheeseCount--; + } + + time++; + } + + System.out.println(time); + } + + static void markExternalAir() { + Queue q = new LinkedList<>(); + boolean[][] visited = new boolean[N][M]; + q.offer(new int[]{0, 0}); + visited[0][0] = true; + external[0][0] = true; + + while (!q.isEmpty()) { + int[] cur = q.poll(); + int y = cur[0], x = cur[1]; + for (int d = 0; d < 4; d++) { + int ny = y + dy[d]; + int nx = x + dx[d]; + if (ny < 0 || nx < 0 || ny >= N || nx >= M) continue; + if (visited[ny][nx]) continue; + if (map[ny][nx] == 1) { + continue; + } + visited[ny][nx] = true; + external[ny][nx] = true; + q.offer(new int[]{ny, nx}); + } + } + } +} +```