From 3199f02de491d3aede4f171c7e81d30d3c9b5554 Mon Sep 17 00:00:00 2001 From: suyeun84 <81475092+suyeun84@users.noreply.github.com> Date: Thu, 28 Aug 2025 16:29:31 +0900 Subject: [PATCH] =?UTF-8?q?[20250828]=20PGM=20/=20LV2=20/=20=EB=AC=B4?= =?UTF-8?q?=EC=9D=B8=EB=8F=84=20=EC=97=AC=ED=96=89=20/=20=EA=B9=80?= =?UTF-8?q?=EC=88=98=EC=97=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\353\217\204 \354\227\254\355\226\211.md" | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 "suyeun84/202508/28 PGM LV2 \353\254\264\354\235\270\353\217\204 \354\227\254\355\226\211.md" diff --git "a/suyeun84/202508/28 PGM LV2 \353\254\264\354\235\270\353\217\204 \354\227\254\355\226\211.md" "b/suyeun84/202508/28 PGM LV2 \353\254\264\354\235\270\353\217\204 \354\227\254\355\226\211.md" new file mode 100644 index 00000000..676b9094 --- /dev/null +++ "b/suyeun84/202508/28 PGM LV2 \353\254\264\354\235\270\353\217\204 \354\227\254\355\226\211.md" @@ -0,0 +1,50 @@ +```java +import java.util.*; +class Solution { + static int[][] map; + static int N, M; + static int[][] dir = new int[][] {{1,0}, {-1,0}, {0,1}, {0,-1}}; + public int[] solution(String[] maps) { + List sums = new ArrayList<>(); + N = maps.length; + M = maps[0].length(); + map = new int[N][M]; + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + char temp = maps[i].charAt(j); + if (temp == 'X') map[i][j] = 0; + else map[i][j] = Integer.parseInt(temp+""); + } + } + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + if (map[i][j] > 0) sums.add(bfs(i, j)); + } + } + if (sums.isEmpty()) return new int[]{-1}; + Collections.sort(sums); + int[] answer = new int[sums.size()]; + for (int i = 0; i < sums.size(); i++) answer[i] = sums.get(i); + return answer; + } + + static int bfs(int y, int x) { + Queue q = new LinkedList<>(); + q.offer(new int[] {y, x}); + int sum = map[y][x]; + map[y][x] = 0; + 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 >= N || ny < 0 || nx >= M || nx < 0 || map[ny][nx] == 0) continue; + q.offer(new int[] {ny, nx}); + sum += map[ny][nx]; + map[ny][nx] = 0; + } + } + return sum; + } +} +```