From 88380406bcd0fed958ee689a0cc3a43af0bdd902 Mon Sep 17 00:00:00 2001 From: JHLEE325 <82587652+JHLEE325@users.noreply.github.com> Date: Fri, 26 Sep 2025 08:35:00 +0900 Subject: [PATCH] =?UTF-8?q?[20250926]=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=EC=9D=B4=EC=A4=80?= =?UTF-8?q?=ED=9D=AC?= 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" | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 "JHLEE325/202509/26 BOJ G4 \353\257\270\353\241\234\353\247\214\353\223\244\352\270\260.md" diff --git "a/JHLEE325/202509/26 BOJ G4 \353\257\270\353\241\234\353\247\214\353\223\244\352\270\260.md" "b/JHLEE325/202509/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..8403138b --- /dev/null +++ "b/JHLEE325/202509/26 BOJ G4 \353\257\270\353\241\234\353\247\214\353\223\244\352\270\260.md" @@ -0,0 +1,64 @@ +```java +import java.io.*; +import java.util.*; + +public class Main { + static int n; + static int[][] map; + static int[][] dist; + static int[] dx = {1, -1, 0, 0}; + static int[] dy = {0, 0, 1, -1}; + static final int INF = 987654321; + + static class Node { + int x, y; + Node(int x, int y) { + this.x = x; + this.y = y; + } + } + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + n = Integer.parseInt(br.readLine()); + map = new int[n][n]; + dist = new int[n][n]; + + for (int i = 0; i < n; i++) { + String line = br.readLine(); + for (int j = 0; j < n; j++) { + map[i][j] = line.charAt(j) - '0'; + dist[i][j] = INF; + } + } + + bfs(); + System.out.println(dist[n - 1][n - 1]); + } + + static void bfs() { + Deque dq = new ArrayDeque<>(); + dist[0][0] = 0; + dq.add(new Node(0, 0)); + + while (!dq.isEmpty()) { + Node cur = dq.pollFirst(); + int x = cur.x, y = cur.y; + + for (int dir = 0; dir < 4; dir++) { + int nx = x + dx[dir]; + int ny = y + dy[dir]; + if (nx < 0 || ny < 0 || nx >= n || ny >= n) continue; + + int cost = (map[nx][ny] == 0 ? 1 : 0); + if (dist[x][y] + cost < dist[nx][ny]) { + dist[nx][ny] = dist[x][y] + cost; + if (cost == 0) dq.addFirst(new Node(nx, ny)); + else dq.addLast(new Node(nx, ny)); + } + } + } + } +} + +```