package com.daily.daily20210129;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
public class minimumEffortPath {
int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
public int minimumEffortPath(int[][] heights) {
int m = heights.length;
int n = heights[0].length;
PriorityQueue<int[]> pq = new PriorityQueue<int[]>((edge1, edge2) -> edge1[2] - edge2[2]);
pq.offer(new int[]{0, 0, 0});
int[] dist = new int[m * n];
Arrays.fill(dist, Integer.MAX_VALUE);
dist[0] = 0;
boolean[] seen = new boolean[m * n];
while (!pq.isEmpty()) {
int[] edge = pq.poll();
int x = edge[0], y = edge[1], d = edge[2];
int id = x * n + y;
if (seen[id]) {
continue;
}
if (x == m - 1 && y == n - 1) {
break;
}
seen[id] = true;
for (int i = 0; i < 4; ++i) {
int nx = x + dirs[i][0];
int ny = y + dirs[i][1];
if (nx >= 0 && nx < m && ny >= 0 && ny < n && Math.max(d, Math.abs(heights[x][y] - heights[nx][ny])) < dist[nx * n + ny]) {
dist[nx * n + ny] = Math.max(d, Math.abs(heights[x][y] - heights[nx][ny]));
pq.offer(new int[]{nx, ny, dist[nx * n + ny]});
}
}
}
return dist[m * n - 1];
}
}