From 5c252d403a169a691a096b23335fd8fbae69d4fe Mon Sep 17 00:00:00 2001 From: suyeun84 <81475092+suyeun84@users.noreply.github.com> Date: Tue, 7 Oct 2025 16:06:50 +0900 Subject: [PATCH] =?UTF-8?q?[20251007]=20PGM=20/=20Lv2=20/=20=EC=84=9D?= =?UTF-8?q?=EC=9C=A0=20=EC=8B=9C=EC=B6=94=20/=20=EA=B9=80=EC=88=98?= =?UTF-8?q?=EC=97=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\354\234\240 \354\213\234\354\266\224.md" | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 "suyeun84/202510/07 PGM LV2 \354\204\235\354\234\240 \354\213\234\354\266\224.md" diff --git "a/suyeun84/202510/07 PGM LV2 \354\204\235\354\234\240 \354\213\234\354\266\224.md" "b/suyeun84/202510/07 PGM LV2 \354\204\235\354\234\240 \354\213\234\354\266\224.md" new file mode 100644 index 00000000..1b8b238c --- /dev/null +++ "b/suyeun84/202510/07 PGM LV2 \354\204\235\354\234\240 \354\213\234\354\266\224.md" @@ -0,0 +1,86 @@ +```java +import java.util.*; +class Solution { + static int[][] visited; + static int[][] dir = new int[][] {{1,0},{-1,0},{0,1},{0,-1}}; + static int N, M, answer; + + static int id; + static int[] size; + + public int solution(int[][] land) { + answer = 0; + N = land.length; + M = land[0].length; + visited = new int[N][M]; + size = new int[N * M + 1]; + id = 0; + + // 0 : 빈 땅, 1 : 석유가 있는 땅 + for (int j = 0; j < M; j++) { + int total = 0; + Set used = new HashSet<>(); + for (int i = 0; i < N; i++) { + if (land[i][j] == 0) continue; + if (visited[i][j] == 0) { + id++; + bfs(i, j, land); + } + int comp = visited[i][j]; + if (!used.contains(comp)) { + used.add(comp); + total += size[comp]; + } + int ny = i; + while (i < N && land[i][j] == 1) i++; + if (i != N) i--; + } + answer = Math.max(answer, total); + } + return answer; + } + + private void bfs(int i, int j, int[][] land) { + Queue q = new LinkedList<>(); + q.add(new Point(i, j)); + visited[i][j] = -1; + int maxVal = 1; + + while (!q.isEmpty()) { + Point cur = q.poll(); + for (int[] d : dir) { + int ny = cur.y + d[0]; + int nx = cur.x + d[1]; + if (ny < 0 || ny >= N || nx < 0 || nx >= M) continue; + if (land[ny][nx] == 0 || visited[ny][nx] != 0) continue; + q.add(new Point(ny, nx)); + visited[ny][nx] = -1; + maxVal++; + } + } + + q.add(new Point(i, j)); + visited[i][j] = id; + while (!q.isEmpty()) { + Point cur = q.poll(); + for (int[] d : dir) { + int ny = cur.y + d[0]; + int nx = cur.x + d[1]; + if (ny < 0 || ny >= N || nx < 0 || nx >= M) continue; + if (visited[ny][nx] != -1) continue; + q.add(new Point(ny, nx)); + visited[ny][nx] = id; + } + } + size[id] = maxVal; + } + + static class Point { + int y, x; + public Point(int y, int x) { + this.y = y; + this.x = x; + } + } +} +```