-
Notifications
You must be signed in to change notification settings - Fork 2
/
path_with_minimum_effort.cpp
53 lines (44 loc) · 1.38 KB
/
path_with_minimum_effort.cpp
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
// 最小体力消耗路径
// https://leetcode.cn/problems/path-with-minimum-effort
// INLINE ../../images/search/path_with_minimum_effort.jpeg
#include <headers.hpp>
class Solution {
public:
int minimumEffortPath(vector<vector<int>> &heights) {
int m = heights.size();
int n = heights[0].size();
vector<vector<int>> effort(m, vector<int>(n, INT_MAX));
priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
greater<>>
pq;
// 方向数组
vector<pair<int, int>> directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
// 初始化
pq.push({0, {0, 0}});
effort[0][0] = 0;
while (!pq.empty()) {
auto [currEffort, currPos] = pq.top();
pq.pop();
int x = currPos.first, y = currPos.second;
// 到达终点
if (x == m - 1 && y == n - 1) {
return currEffort;
}
for (auto &dir : directions) {
int nx = x + dir.first;
int ny = y + dir.second;
// 检查边界
if (nx >= 0 && nx < m && ny >= 0 && ny < n) {
// 计算新的努力值
int newEffort = max(currEffort, abs(heights[x][y] - heights[nx][ny]));
// 如果找到更小的努力值
if (newEffort < effort[nx][ny]) {
effort[nx][ny] = newEffort;
pq.push({newEffort, {nx, ny}});
}
}
}
}
return 0;
}
};