From e97b437e2b10e64dfce205a8266612267ee18520 Mon Sep 17 00:00:00 2001 From: suyeun84 <81475092+suyeun84@users.noreply.github.com> Date: Thu, 18 Sep 2025 23:29:12 +0900 Subject: [PATCH] =?UTF-8?q?[20250918]=20PGM=20/=20LV2=20/=20=EC=A7=80?= =?UTF-8?q?=EA=B2=8C=EC=B0=A8=EC=99=80=20=ED=81=AC=EB=A0=88=EC=9D=B8=20/?= =?UTF-8?q?=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 --- ...0 \355\201\254\353\240\210\354\235\270.md" | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 "suyeun84/202509/18 PGM LV2 \354\247\200\352\262\214\354\260\250\354\231\200 \355\201\254\353\240\210\354\235\270.md" diff --git "a/suyeun84/202509/18 PGM LV2 \354\247\200\352\262\214\354\260\250\354\231\200 \355\201\254\353\240\210\354\235\270.md" "b/suyeun84/202509/18 PGM LV2 \354\247\200\352\262\214\354\260\250\354\231\200 \355\201\254\353\240\210\354\235\270.md" new file mode 100644 index 00000000..456d5871 --- /dev/null +++ "b/suyeun84/202509/18 PGM LV2 \354\247\200\352\262\214\354\260\250\354\231\200 \355\201\254\353\240\210\354\235\270.md" @@ -0,0 +1,63 @@ +```java +import java.util.*; +class Solution { + static int[][] dir = new int[][] {{1,0}, {-1,0}, {0,1}, {0,-1}}; + static int N, M; + static char[][] stor; + public int solution(String[] storage, String[] requests) { + int answer = 0; + N = storage.length; + M = storage[0].length(); + stor = new char[N][M]; + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + stor[i][j] = storage[i].charAt(j); + } + } + for (String request : requests) { + int type = request.length(); + if (type == 1) { + ArrayList arr = new ArrayList<>(); + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + if (stor[i][j] != request.charAt(0)) continue; + if (i == 0 || i == N-1 || j == 0 || j == M-1 || check(i, j)) arr.add(new int[]{i, j}); + } + } + for (int[] a : arr) stor[a[0]][a[1]] = '0'; + } else if (type == 2) { + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + if (stor[i][j] == request.charAt(0)) stor[i][j] = '0'; + } + } + } + } + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + if (stor[i][j] != '0') answer++; + } + } + return answer; + } + + static boolean check(int i, int j) { + boolean[][] visited = new boolean[N][M]; + Queue q = new LinkedList<>(); + q.add(new int[] {i, j}); + visited[i][j] = true; + while (!q.isEmpty()) { + int[] curr = q.poll(); + for (int[] d : dir) { + int ny = curr[0] + d[0]; + int nx = curr[1] + d[1]; + if (ny < 0 || ny >= N || nx < 0 || nx >= M || visited[ny][nx] || stor[ny][nx] != '0') continue; + if (ny == 0 || ny == N-1 || nx == 0 || nx == M-1) return true; + q.add(new int[] {ny, nx}); + visited[ny][nx] = true; + } + } + return false; + } +} +```