From 6d147d119702e163d9c9ed36f511aabe63da38a6 Mon Sep 17 00:00:00 2001 From: zinnnn37 Date: Mon, 15 Dec 2025 22:30:55 +0900 Subject: [PATCH 1/2] =?UTF-8?q?[20251215]=20BOJ=20/=20G2=20/=20=EB=B2=BD?= =?UTF-8?q?=20=EB=B6=80=EC=88=98=EA=B3=A0=20=EC=9D=B4=EB=8F=99=ED=95=98?= =?UTF-8?q?=EA=B8=B0=204=20/=20=EA=B9=80=EB=AF=BC=EC=A7=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\353\217\231\355\225\230\352\270\260 4.md" | 122 ++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 "zinnnn37/202512/15 BOJ G2 \353\262\275 \353\266\200\354\210\230\352\263\240 \354\235\264\353\217\231\355\225\230\352\270\260 4.md" diff --git "a/zinnnn37/202512/15 BOJ G2 \353\262\275 \353\266\200\354\210\230\352\263\240 \354\235\264\353\217\231\355\225\230\352\270\260 4.md" "b/zinnnn37/202512/15 BOJ G2 \353\262\275 \353\266\200\354\210\230\352\263\240 \354\235\264\353\217\231\355\225\230\352\270\260 4.md" new file mode 100644 index 00000000..47cffe34 --- /dev/null +++ "b/zinnnn37/202512/15 BOJ G2 \353\262\275 \353\266\200\354\210\230\352\263\240 \354\235\264\353\217\231\355\225\230\352\270\260 4.md" @@ -0,0 +1,122 @@ +```java +import java.awt.*; +import java.io.*; +import java.util.*; +import java.util.List; +import java.util.Queue; + +public class BJ_16946_벽_부수고_이동하기_4 { + + private static final int[] dx = { 0, 1, 0, -1 }; + private static final int[] dy = { 1, 0, -1, 0 }; + + private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + private static StringBuilder sb = new StringBuilder(); + private static StringTokenizer st; + + private static int N, M, index; + private static int[][] matrix; + private static boolean[][] visited; + private static Queue q; + private static List list; + private static Map groups; + + public static void main(String[] args) throws IOException { + init(); + sol(); + } + + private static void init() throws IOException { + st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + index = 2; + + matrix = new int[N][M]; + for (int i = 0; i < N; i++) { + String input = br.readLine(); + for (int j = 0; j < M; j++) { + matrix[i][j] = input.charAt(j) - '0'; + } + } + visited = new boolean[N][M]; + q = new ArrayDeque<>(); + list = new ArrayList<>(); + groups = new HashMap<>(); + } + + private static void sol() throws IOException { + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + if (matrix[i][j] == 0 && !visited[i][j]) { + bfs(i, j); + index++; + } + } + } + + printMat(); + + bw.write(sb.toString()); + bw.flush(); + bw.close(); + br.close(); + } + + private static void bfs(int i, int j) { + q.clear(); + q.offer(new Point(i, j)); + visited[i][j] = true; + matrix[i][j] = index; + + int cnt = 1; + while (!q.isEmpty()) { + Point cur = q.poll(); + + for (int d = 0; d < 4; d++) { + int nx = cur.x + dx[d]; + int ny = cur.y + dy[d]; + + if (OOB(nx, ny) || visited[nx][ny] || matrix[nx][ny] != 0) continue; + + visited[nx][ny] = true; + matrix[nx][ny] = index; + cnt++; + q.offer(new Point(nx, ny)); + } + } + groups.put(index, cnt); + } + + private static void printMat() { + int cnt; + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + if (matrix[i][j] != 1) { + sb.append(0); + } else { + cnt = 1; + list.clear(); + for (int d = 0; d < 4; d++) { + int nx = i + dx[d]; + int ny = j + dy[d]; + + if (OOB(nx, ny) || matrix[nx][ny] == 1 || list.contains(matrix[nx][ny])) continue; + + list.add(matrix[nx][ny]); + cnt += groups.get(matrix[nx][ny]); + } + sb.append(cnt % 10); + } + } + sb.append("\n"); + } + } + + private static boolean OOB(int x, int y) { + return x < 0 || N <= x || y < 0 || M <= y; + } + +} +``` \ No newline at end of file From 689cd3ad71d9fa33f9a9a8a19129250ad9463b25 Mon Sep 17 00:00:00 2001 From: zinnnn37 Date: Mon, 15 Dec 2025 22:33:54 +0900 Subject: [PATCH 2/2] =?UTF-8?q?[20251215]=20BOJ=20/=20G2=20/=20=EB=B2=BD?= =?UTF-8?q?=20=EB=B6=80=EC=88=98=EA=B3=A0=20=EC=9D=B4=EB=8F=99=ED=95=98?= =?UTF-8?q?=EA=B8=B0=204=20/=20=EA=B9=80=EB=AF=BC=EC=A7=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\235\264\353\217\231\355\225\230\352\270\260 4.md" | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git "a/zinnnn37/202512/15 BOJ G2 \353\262\275 \353\266\200\354\210\230\352\263\240 \354\235\264\353\217\231\355\225\230\352\270\260 4.md" "b/zinnnn37/202512/15 BOJ G2 \353\262\275 \353\266\200\354\210\230\352\263\240 \354\235\264\353\217\231\355\225\230\352\270\260 4.md" index 47cffe34..99a9959a 100644 --- "a/zinnnn37/202512/15 BOJ G2 \353\262\275 \353\266\200\354\210\230\352\263\240 \354\235\264\353\217\231\355\225\230\352\270\260 4.md" +++ "b/zinnnn37/202512/15 BOJ G2 \353\262\275 \353\266\200\354\210\230\352\263\240 \354\235\264\353\217\231\355\225\230\352\270\260 4.md" @@ -2,7 +2,6 @@ import java.awt.*; import java.io.*; import java.util.*; -import java.util.List; import java.util.Queue; public class BJ_16946_벽_부수고_이동하기_4 { @@ -19,7 +18,7 @@ public class BJ_16946_벽_부수고_이동하기_4 { private static int[][] matrix; private static boolean[][] visited; private static Queue q; - private static List list; + private static Set set; private static Map groups; public static void main(String[] args) throws IOException { @@ -42,7 +41,7 @@ public class BJ_16946_벽_부수고_이동하기_4 { } visited = new boolean[N][M]; q = new ArrayDeque<>(); - list = new ArrayList<>(); + set = new HashSet<>(); groups = new HashMap<>(); } @@ -97,14 +96,14 @@ public class BJ_16946_벽_부수고_이동하기_4 { sb.append(0); } else { cnt = 1; - list.clear(); + set.clear(); for (int d = 0; d < 4; d++) { int nx = i + dx[d]; int ny = j + dy[d]; - if (OOB(nx, ny) || matrix[nx][ny] == 1 || list.contains(matrix[nx][ny])) continue; + if (OOB(nx, ny) || matrix[nx][ny] == 1 || set.contains(matrix[nx][ny])) continue; - list.add(matrix[nx][ny]); + set.add(matrix[nx][ny]); cnt += groups.get(matrix[nx][ny]); } sb.append(cnt % 10);