Skip to content

Commit 3435f58

Browse files
authored
[20250208] BOJ / 골드4 / 창영이와 퇴근 / 이강현
1 parent 36f555d commit 3435f58

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
class Node implements Comparable<Node> {
6+
int i, j, maxSlope;
7+
8+
Node(int i, int j, int maxSlope) {
9+
this.i = i;
10+
this.j = j;
11+
this.maxSlope = maxSlope;
12+
}
13+
14+
@Override
15+
public int compareTo(Node other) {
16+
return Integer.compare(this.maxSlope, other.maxSlope); // 최소 경사값을 기준으로 정렬
17+
}
18+
}
19+
20+
public class Main {
21+
static int N;
22+
static int[][] grid;
23+
static int[][] dist;
24+
static int[] di = {-1, 1, 0, 0};
25+
static int[] dj = {0, 0, -1, 1};
26+
27+
public static void main(String[] args) throws IOException {
28+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
29+
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
30+
31+
N = Integer.parseInt(br.readLine());
32+
grid = new int[N][N];
33+
dist = new int[N][N];
34+
35+
for (int i = 0; i < N; i++) {
36+
StringTokenizer st = new StringTokenizer(br.readLine());
37+
for (int j = 0; j < N; j++) {
38+
grid[i][j] = Integer.parseInt(st.nextToken());
39+
dist[i][j] = Integer.MAX_VALUE; // 최대 경사를 저장하므로, 최댓값으로 초기화
40+
}
41+
}
42+
43+
dijkstra();
44+
45+
bw.write(dist[N - 1][N - 1] + "\n");
46+
bw.flush();
47+
br.close();
48+
bw.close();
49+
}
50+
51+
public static void dijkstra() {
52+
PriorityQueue<Node> pq = new PriorityQueue<>();
53+
pq.add(new Node(0, 0, 0)); // 시작 위치, 초기 경사 0
54+
dist[0][0] = 0;
55+
56+
while (!pq.isEmpty()) {
57+
Node current = pq.poll();
58+
59+
if (current.maxSlope > dist[current.i][current.j]) continue; // 기존 값보다 크면 무시
60+
61+
for (int d = 0; d < 4; d++) {
62+
int newi = current.i + di[d];
63+
int newj = current.j + dj[d];
64+
65+
if (newi >= 0 && newi < N && newj >= 0 && newj < N) {
66+
int slope = Math.abs(grid[newi][newj] - grid[current.i][current.j]); // 경사 계산
67+
int newMaxSlope = Math.max(current.maxSlope, slope); // 경로 중 최대 경사 갱신
68+
69+
if (dist[newi][newj] > newMaxSlope) { // 더 작은 최대 경사값을 찾으면 갱신
70+
dist[newi][newj] = newMaxSlope;
71+
pq.add(new Node(newi, newj, newMaxSlope));
72+
}
73+
}
74+
}
75+
}
76+
}
77+
}
78+
79+
```

0 commit comments

Comments
 (0)