From 7ba3183db9f868a19c23f0ae302b141891e8edd2 Mon Sep 17 00:00:00 2001 From: zinnnn37 Date: Sat, 3 Jan 2026 22:07:51 +0900 Subject: [PATCH] =?UTF-8?q?[20260103]=20BOJ=20/=20G4=20/=20=EC=A3=BC?= =?UTF-8?q?=EB=82=9C=EC=9D=98=20=EB=82=9C=20/=20=EA=B9=80=EB=AF=BC?= =?UTF-8?q?=EC=A7=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\353\202\234\354\235\230 \353\202\234.md" | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 "zinnnn37/202601/3 BOJ G4 \354\243\274\353\202\234\354\235\230 \353\202\234.md" diff --git "a/zinnnn37/202601/3 BOJ G4 \354\243\274\353\202\234\354\235\230 \353\202\234.md" "b/zinnnn37/202601/3 BOJ G4 \354\243\274\353\202\234\354\235\230 \353\202\234.md" new file mode 100644 index 00000000..f8696e20 --- /dev/null +++ "b/zinnnn37/202601/3 BOJ G4 \354\243\274\353\202\234\354\235\230 \353\202\234.md" @@ -0,0 +1,112 @@ +```java +import java.io.*; +import java.util.ArrayDeque; +import java.util.Arrays; +import java.util.Deque; +import java.util.StringTokenizer; + +public class BJ_14497_μ£Όλ‚œμ˜_λ‚œ { + + private static final int INF = 987654321; + private static final int[] dx = { -1, 1, 0, 0 }; + private static final int[] dy = { 0, 0, -1, 1 }; + + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + private static StringTokenizer st; + + private static int N, M; + private static int[] points; + private static int[][] dist; + private static char[][] matrix; + private static Deque q; + + private static class Node { + int x; + int y; + int cnt; + + public Node(int x, int y, int cnt) { + this.x = x; + this.y = y; + this.cnt = cnt; + } + + } + + public static void main(String[] args) throws IOException { + init(); + sol(); + + bw.flush(); + bw.close(); + br.close(); + } + + private static void init() throws IOException { + st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + + points = new int[4]; + st = new StringTokenizer(br.readLine()); + for (int i = 0; i < 4; i++) { + points[i] = Integer.parseInt(st.nextToken()) - 1; + } + + dist = new int[N][M]; + for (int i = 0; i < N; i++) { + Arrays.fill(dist[i], INF); + } + + matrix = new char[N][M]; + for (int i = 0; i < N; i++) { + String line = br.readLine(); + for (int j = 0; j < M; j++) { + matrix[i][j] = line.charAt(j); + } + } + q = new ArrayDeque<>(); + } + + private static void sol() throws IOException { + q.offer(new Node(points[0], points[1], 0)); + dist[points[0]][points[1]] = 0; + + while (!q.isEmpty()) { + Node cur = q.poll(); + + if (dist[cur.x][cur.y] < cur.cnt) continue; + + if (matrix[cur.x][cur.y] == '#') { + bw.write(cur.cnt + ""); + return; + } + + for (int d = 0; d < 4; d++) { + int nx = cur.x + dx[d]; + int ny = cur.y + dy[d]; + + if (OOB(nx, ny)) continue; + + int newDist = cur.cnt + (matrix[nx][ny] == '0' ? 0 : 1); + + if (newDist < dist[nx][ny]) { + dist[nx][ny] = newDist; + + if (matrix[nx][ny] == '0') { + q.offerFirst(new Node(nx, ny, newDist)); + } else { + q.offerLast(new Node(nx, ny, newDist)); + } + } + } + } + } + + private static boolean OOB(int x, int y) { + return x < 0 || N <= x || y < 0 || M <= y; + } + +} +```