From d2783b855aba423d7b2fdda799c1c9da7795cf5c Mon Sep 17 00:00:00 2001 From: suyeun84 <81475092+suyeun84@users.noreply.github.com> Date: Mon, 18 Aug 2025 15:42:15 +0900 Subject: [PATCH] =?UTF-8?q?[20250818]=20BOJ=20/=20G4=20/=20=EC=B9=98?= =?UTF-8?q?=EC=A6=88=20/=20=EA=B9=80=EC=88=98=EC=97=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../18 BOJ G4 \354\271\230\354\246\210.md" | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 "suyeun84/202508/18 BOJ G4 \354\271\230\354\246\210.md" diff --git "a/suyeun84/202508/18 BOJ G4 \354\271\230\354\246\210.md" "b/suyeun84/202508/18 BOJ G4 \354\271\230\354\246\210.md" new file mode 100644 index 00000000..93ccb8b6 --- /dev/null +++ "b/suyeun84/202508/18 BOJ G4 \354\271\230\354\246\210.md" @@ -0,0 +1,61 @@ +```java +import java.util.*; +import java.io.*; + +public class boj2636 { + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static StringTokenizer st; + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} + static int nextInt() {return Integer.parseInt(st.nextToken());} + + static int N, M, time = 0, answer = 0; + static int[][] map; + static int[][] dir = new int[][]{{1,0}, {-1,0}, {0,1}, {0,-1}}; + public static void main(String[] args) throws Exception { + nextLine(); + N = nextInt(); + M = nextInt(); + map = new int[N][M]; + for (int i = 0; i < N; i++) { + nextLine(); + for (int j = 0; j < M; j++) { + map[i][j] = nextInt(); + if (map[i][j] == 1) answer++; + } + } + while (answer > 0) { + time++; + int melted = solve(); + if (answer - melted == 0) break; + answer -= melted; + } + System.out.println(time); + System.out.println(answer); + } + // 밖이랑 연결되어 있는지 확인 + static int solve() { + Queue q = new LinkedList<>(); + boolean[][] visited = new boolean[N][M]; + q.add(new int[]{0, 0}); + int melted = 0; + visited[0][0] = true; + while(!q.isEmpty()) { + int[] cur = q.poll(); + for (int[] d : dir) { + int ny = cur[0] + d[0]; + int nx = cur[1] + d[1]; + if (ny < 0 || ny >= N || nx < 0 || nx >= M || visited[ny][nx]) continue; + if (map[ny][nx] == 0) { + visited[ny][nx] = true; + q.add(new int[] {ny, nx}); + } else { + map[ny][nx] = 0; + visited[ny][nx] = true; + melted++; + } + } + } + return melted; + } +} +```