-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpathWithMinimumEffort.cpp
More file actions
75 lines (73 loc) · 2.42 KB
/
pathWithMinimumEffort.cpp
File metadata and controls
75 lines (73 loc) · 2.42 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Source: https://leetcode.com/problems/path-with-minimum-effort/
// Author: Miao Zhang
// Date: 2021-05-24
class Solution {
public:
int minimumEffortPath(vector<vector<int>>& heights) {
int m = heights.size();
int n = heights[0].size();
vector<vector<int>> dirs{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
auto bfs = [&] (int cost) -> bool {
queue<pair<int, int>> q;
vector<int> seen(m * n);
q.emplace(0, 0);
seen[0] = 1;
while (!q.empty()) {
auto [x, y] = q.front();
q.pop();
if (x == m - 1 && y == n - 1) return true;
for (auto d: dirs) {
int dx = x + d[0];
int dy = y + d[1];
if (dx < 0 || dx >= m || dy < 0 || dy >= n) continue;
if (abs(heights[dx][dy] - heights[x][y]) > cost) continue;
if (seen[dx * n + dy]) continue;
seen[dx * n + dy]++;
q.emplace(dx, dy);
}
}
return false;
};
int l = 0;
int r = 1e6 + 1;
while (l < r) {
int mid = l + (r - l) / 2;
if (bfs(mid)) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
}
};
class Solution {
public:
int minimumEffortPath(vector<vector<int>>& heights) {
int m = heights.size();
int n = heights[0].size();
vector<vector<int>> dirs{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
vector<int> dist(m * n, INT_MAX / 2);
q.emplace(0, 0);
dist[0] = 0;
while (!q.empty()) {
auto [d, cur] = q.top();
q.pop();
if (cur == m * n - 1) return d;
if (d > dist[cur]) continue;
int x = cur / n;
int y = cur % n;
for (auto dir: dirs) {
int dx = x + dir[0];
int dy = y + dir[1];
if (dx < 0 || dx >= m || dy < 0 || dy >= n) continue;
int nxt = dx * n + dy;
int newd = abs(heights[dx][dy] - heights[x][y]);
if (max(d, newd) >= dist[nxt]) continue;
q.emplace(dist[nxt] = max(d, newd), nxt);
}
}
return -1;
}
};