-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path1631.cpp
More file actions
33 lines (27 loc) · 1.05 KB
/
Copy path1631.cpp
File metadata and controls
33 lines (27 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution {
typedef tuple<unsigned, uint8_t, uint8_t> tile;
public:
int minimumEffortPath(const vector<vector<int>> &heights) {
static unsigned efforts[101][101];
const int n = heights.size(), m = heights[0].size();
static const int dirs[5] = {-1, 0, 1, 0, -1};
memset(efforts, 0xFF, sizeof(efforts));
priority_queue<tile, vector<tile>, greater<tile>> pq;
pq.push({0, 0, 0});
while (!pq.empty()) {
const auto [effort, x, y] = pq.top();
pq.pop();
if (x == n - 1 && y == m - 1) return effort;
for (int k = 0; k < 4; k++) {
const int i = x + dirs[k], j = y + dirs[k + 1];
if (i < 0 || i >= n || j < 0 || j >= m) continue;
const unsigned crnt = max(effort, (unsigned)abs(heights[x][y] - heights[i][j]));
if (crnt < efforts[i][j]) {
efforts[i][j] = crnt;
pq.push({crnt, i, j});
}
}
}
return -1;
}
};