From 3435f5872b6b9fd933804a15ec1b5d5eeb92f304 Mon Sep 17 00:00:00 2001 From: lkhyun <102892446+lkhyun@users.noreply.github.com> Date: Sat, 8 Feb 2025 01:08:30 +0900 Subject: [PATCH] =?UTF-8?q?[20250208]=20BOJ=20/=20=EA=B3=A8=EB=93=9C4=20/?= =?UTF-8?q?=20=EC=B0=BD=EC=98=81=EC=9D=B4=EC=99=80=20=ED=87=B4=EA=B7=BC=20?= =?UTF-8?q?/=20=EC=9D=B4=EA=B0=95=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\354\231\200 \355\207\264\352\267\274.md" | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 "lkhyun/202502/08 BOJ \352\263\250\353\223\2344 \354\260\275\354\230\201\354\235\264\354\231\200 \355\207\264\352\267\274.md" diff --git "a/lkhyun/202502/08 BOJ \352\263\250\353\223\2344 \354\260\275\354\230\201\354\235\264\354\231\200 \355\207\264\352\267\274.md" "b/lkhyun/202502/08 BOJ \352\263\250\353\223\2344 \354\260\275\354\230\201\354\235\264\354\231\200 \355\207\264\352\267\274.md" new file mode 100644 index 00000000..f9fea330 --- /dev/null +++ "b/lkhyun/202502/08 BOJ \352\263\250\353\223\2344 \354\260\275\354\230\201\354\235\264\354\231\200 \355\207\264\352\267\274.md" @@ -0,0 +1,79 @@ +```java +import java.io.*; +import java.util.*; + +class Node implements Comparable { + int i, j, maxSlope; + + Node(int i, int j, int maxSlope) { + this.i = i; + this.j = j; + this.maxSlope = maxSlope; + } + + @Override + public int compareTo(Node other) { + return Integer.compare(this.maxSlope, other.maxSlope); // 최소 경사값을 기준으로 정렬 + } +} + +public class Main { + static int N; + static int[][] grid; + static int[][] dist; + static int[] di = {-1, 1, 0, 0}; + static int[] dj = {0, 0, -1, 1}; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + + N = Integer.parseInt(br.readLine()); + grid = new int[N][N]; + dist = new int[N][N]; + + for (int i = 0; i < N; i++) { + StringTokenizer st = new StringTokenizer(br.readLine()); + for (int j = 0; j < N; j++) { + grid[i][j] = Integer.parseInt(st.nextToken()); + dist[i][j] = Integer.MAX_VALUE; // 최대 경사를 저장하므로, 최댓값으로 초기화 + } + } + + dijkstra(); + + bw.write(dist[N - 1][N - 1] + "\n"); + bw.flush(); + br.close(); + bw.close(); + } + + public static void dijkstra() { + PriorityQueue pq = new PriorityQueue<>(); + pq.add(new Node(0, 0, 0)); // 시작 위치, 초기 경사 0 + dist[0][0] = 0; + + while (!pq.isEmpty()) { + Node current = pq.poll(); + + if (current.maxSlope > dist[current.i][current.j]) continue; // 기존 값보다 크면 무시 + + for (int d = 0; d < 4; d++) { + int newi = current.i + di[d]; + int newj = current.j + dj[d]; + + if (newi >= 0 && newi < N && newj >= 0 && newj < N) { + int slope = Math.abs(grid[newi][newj] - grid[current.i][current.j]); // 경사 계산 + int newMaxSlope = Math.max(current.maxSlope, slope); // 경로 중 최대 경사 갱신 + + if (dist[newi][newj] > newMaxSlope) { // 더 작은 최대 경사값을 찾으면 갱신 + dist[newi][newj] = newMaxSlope; + pq.add(new Node(newi, newj, newMaxSlope)); + } + } + } + } + } +} + +```