前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode 1631. 最小体力消耗路径(最短路径)

LeetCode 1631. 最小体力消耗路径(最短路径)

作者头像
SakuraTears
发布2022-01-13 14:03:14
2470
发布2022-01-13 14:03:14
举报
文章被收录于专栏:从零开始的Code生活

题目

你准备参加一场远足活动。给你一个二维 rows x columns 的地图 heights ,其中 heightsrow 表示格子 (row, col) 的高度。一开始你在最左上角的格子 (0, 0) ,且你希望去最右下角的格子 (rows-1, columns-1) (注意下标从 0 开始编号)。你每次可以往 上,下,左,右 四个方向之一移动,你想要找到耗费 体力 最小的一条路径。

一条路径耗费的 体力值 是路径上相邻格子之间 高度差绝对值 的 最大值 决定的。

请你返回从左上角走到右下角的最小 体力消耗值 。

示例 1:

0
0

输入:heights = [[1,2,2],[3,8,2],[5,3,5]] 输出:2 解释:路径 [1,3,5,3,5] 连续格子的差值绝对值最大为 2 。 这条路径比路径 [1,2,2,2,5] 更优,因为另一条路径差值最大值为 3 。 示例 2:

0
0

输入:heights = [[1,2,3],[3,8,4],[5,3,5]] 输出:1 解释:路径 [1,2,3,4,5] 的相邻格子差值绝对值最大为 1 ,比路径 [1,3,5,3,5] 更优。 示例 3:

0
0

输入:heights = [[1,2,1,1,1],[1,2,1,2,1],[1,2,1,2,1],[1,2,1,2,1],[1,1,1,2,1]] 输出:0 解释:上图所示路径不需要消耗任何体力。

提示:

rows == heights.length columns == heights[i].length 1 <= rows, columns <= 100 1 <= heightsi <= 106

来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/path-with-minimum-effort 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路:最短路径(迪杰斯特拉)

代码语言:javascript
复制
class Solution {
private:
 static constexpr int dirs[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
 
public:
 int minimumEffortPath(vector<vector<int>>& heights) {
 int m = heights.size(), n = heights[0].size();
        vector<int> form(m * n, INT_MAX);
        queue<pair<int, int>> q;
        q.emplace(0, 0);
        form[0] = 0;
 while (!q.empty()) {
 auto [x, y] = q.front();
            q.pop();
 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) {
 int temp = max(abs(heights[nx][ny] - heights[x][y]), form[x * n + y]);
 if (temp >= form[nx * n + ny]) continue;
                    form[nx * n + ny] = temp;
                    q.emplace(nx, ny);
                }
            }
        }
 return form[m * n - 1];
    }
};

思路:二分

将此问题转换成一个“是否存在一条从左上角到右下角的路径,其体力消耗最大值小于x”问题

采用搜索方法遍历所有路径,当两个点之间消耗的体力值大于x时则不可以到达

代码语言:javascript
复制
class Solution {
private:
    static constexpr int dirs[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    
public:
    int minimumEffortPath(vector<vector<int>>& heights) {
        int m = heights.size();
        int n = heights[0].size();
        int left = 0, right = 999999, ans = 0;
        while (left <= right) {
            int mid = (left + right) / 2;
            queue<pair<int, int>> q;
            q.emplace(0, 0);
            vector<int> seen(m * n);
            seen[0] = 1;
            while (!q.empty()) {
                auto [x, y] = q.front();
                q.pop();
                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 && !seen[nx * n + ny] && abs(heights[x][y] - heights[nx][ny]) <= mid) {
                        q.emplace(nx, ny);
                        seen[nx * n + ny] = 1;
                    }
                }
            }
            if (seen[m * n - 1]) {
                ans = mid;
                right = mid - 1;
            }
            else {
                left = mid + 1;
            }
        }
        return ans;
    }
};
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021年01月30日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目
  • 思路:最短路径(迪杰斯特拉)
  • 思路:二分
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档