From 66d492f71ee370c37fc4a6a65dc1fd0825e7a907 Mon Sep 17 00:00:00 2001 From: zinnnn37 Date: Fri, 26 Dec 2025 23:13:27 +0900 Subject: [PATCH] =?UTF-8?q?[20251226]=20BOJ=20/=20G4=20/=20=EB=AF=B8?= =?UTF-8?q?=EB=A1=9C=EB=A7=8C=EB=93=A4=EA=B8=B0=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 --- ...34\353\247\214\353\223\244\352\270\260.md" | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 "zinnnn37/202512/26 BOJ G4 \353\257\270\353\241\234\353\247\214\353\223\244\352\270\260.md" diff --git "a/zinnnn37/202512/26 BOJ G4 \353\257\270\353\241\234\353\247\214\353\223\244\352\270\260.md" "b/zinnnn37/202512/26 BOJ G4 \353\257\270\353\241\234\353\247\214\353\223\244\352\270\260.md" new file mode 100644 index 00000000..d15599e5 --- /dev/null +++ "b/zinnnn37/202512/26 BOJ G4 \353\257\270\353\241\234\353\247\214\353\223\244\352\270\260.md" @@ -0,0 +1,102 @@ +```java +package etc; + +import java.io.*; +import java.util.ArrayDeque; +import java.util.Arrays; +import java.util.Deque; + +public class BJ_2665_미로만들기 { + + private static final int[] dx = { 0, 0, -1, 1 }; + private static final int[] dy = { -1, 1, 0, 0 }; + + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + + private static int N; + private static int[][] dist; + private static char[][] matrix; + private static Deque dq; + + private static class Node { + int x; + int y; + int cnt; + + 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(); + } + + private static void init() throws IOException { + N = Integer.parseInt(br.readLine()); + + matrix = new char[N][N]; + for (int i = 0; i < N; i++) { + String s = br.readLine(); + for (int j = 0; j < N; j++) { + matrix[i][j] = s.charAt(j); + } + } + + dist = new int[N][N]; + for (int[] row : dist) { + Arrays.fill(row, Integer.MAX_VALUE); + } + + dq = new ArrayDeque<>(); + } + + private static void sol() throws IOException { + dq.offer(new Node(0, 0, 0)); + dist[0][0] = 0; + + while (!dq.isEmpty()) { + Node cur = dq.poll(); + + if (cur.x == N - 1 && cur.y == N - 1) { + bw.write(cur.cnt + ""); + bw.flush(); + bw.close(); + br.close(); + return; + } + + if (cur.cnt > dist[cur.x][cur.y]) continue; + + 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 newCost = cur.cnt + (matrix[nx][ny] == '0' ? 1 : 0); + + if (newCost < dist[nx][ny]) { + dist[nx][ny] = newCost; + + if (matrix[nx][ny] == '1') { + dq.offerFirst(new Node(nx, ny, newCost)); + } else { + dq.offerLast(new Node(nx, ny, newCost)); + } + } + } + } + } + + private static boolean OOB(int x, int y) { + return x < 0 || N <= x || y < 0 || N <= y; + } + +} +``` \ No newline at end of file