链接:1631. 最小体力消耗路径

题解:https://leetcode-cn.com/problems/path-with-minimum-effort/solution/duo-tu-xiang-xi-fen-xi-jie-ti-si-lu-fen-7z89x/

https://leetcode-cn.com/problems/path-with-minimum-effort/solution/zui-xiao-ti-li-xiao-hao-lu-jing-by-zerotrac2/

class Solution {private:vector<vector<int>> direction{{1, 0}, {0, 1}, {-1, 0}, {0, -1}};bool dfs(vector<vector<int>>& grid, int row, int col, vector<vector<bool>>& visited, int val) {int m = grid.size();int n = grid[0].size();visited[row][col] = true;for(int i = 0; i < 4; ++i) {int next_row = row + direction[i][0];int next_col = col + direction[i][1];// check是否超过边界,并且当前路线是否满足<=valif(next_row < 0 || next_row >= m || next_col < 0 || next_col >= n || visited[next_row][next_col] || abs(grid[next_row][next_col]-grid[row][col]) > val) {continue;}// 到达重点。表示val数值可以if(next_row == m-1 && next_col == n-1) {return true;}// 搜索下一层if(dfs(grid, next_row, next_col, visited, val)) {return true;}}return false;}public:int minimumEffortPath(vector<vector<int>>& heights) {if(heights[0].size() == 1 && heights.size() == 1) {return 0;}int low = 0;int high = 1000000;while(low < high) {int mid = (high+low)/2;vector<vector<bool>> visited(heights.size(), vector<bool>(heights[0].size(), false));// 如果mid可以,需要继续缩小边界if(dfs(heights, 0, 0, visited, mid)) {high = mid;} else {low = mid+1;}}return low;}
};

1631. 最小体力消耗路径相关推荐

  1. leetcode 1631. 最小体力消耗路径(并查集)

    你准备参加一场远足活动.给你一个二维 rows x columns 的地图 heights ,其中 heights[row][col] 表示格子 (row, col) 的高度.一开始你在最左上角的格子 ...

  2. LeetCode 1631. 最小体力消耗路径(DFS + 二分查找)

    文章目录 1. 题目 2. 解题 1. 题目 你准备参加一场远足活动.给你一个二维 rows x columns 的地图 heights ,其中 heights[row][col] 表示格子 (row ...

  3. 【LeetCode】1631. Path With Minimum Effort 最小体力消耗路径(Medium)(JAVA)每日一题

    [LeetCode]1631. Path With Minimum Effort 最小体力消耗路径(Medium)(JAVA) 题目描述: You are a hiker preparing for ...

  4. LeetCode 64. Minimum Path Sum(最小和的路径)

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  5. 有n堆石子,每次取出两堆合成一堆,每堆石子的个数即为合并石子所需要耗费的体力,求出合并所有石子堆所需要耗费的最小体力

    有n堆石子,每次取出两堆合成一堆,每堆石子的个数即为合并石子所需要耗费的体力,求出合并所有石子堆所需要耗费的最小体力 典型的贪心题,即每次取出数量最少的两堆石子合并. 举个例子来说,假如有5堆石子,石 ...

  6. hdu 5092 线裁剪(纵向连线最小和+输出路径)

    http://acm.hdu.edu.cn/showproblem.php?pid=5092 给一个m*n的矩阵,找到一个纵向的"线"使得线上的和最小并输出这条线,线能向8个方向延 ...

  7. 三角形最小/最大路径

    题目描述: 给定一个三角形,找出自顶向下的最小路径和.每一步只能移动到下一行中相邻的结点上. 例如,给定三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] 自顶向下的最小路 ...

  8. leetcode 931. Minimum Falling Path Sum(最小的下降路径和)

    下降的路径就是从第一行任一个元素出发,只能直着或斜着走到下一行,直到到达最后一行,返回形成的路径中最小的路径和. 思路: DP 从下往上推, 在最后一行选一个元素,那么它是从哪里过来的?是从上一行的j ...

  9. 最短路径(Shortest Paths)

    最短路径 最短路径(Shortest Paths) 最短路径问题一直是图论研究的热点问题.例如在实际生活中的路径规划.地图导航等领域有重要的应用.关于求解图的最短路径方法也层出不穷,本篇文章将详细讲解 ...

最新文章

  1. 凭证 90000000 保存(帐户确定出错)
  2. 推荐算法工程师成长2:排序模块
  3. Java 源码赏析 - java.lang - Void
  4. Intel PAUSE指令变化影响到MySQL的性能,该如何解决?
  5. 坏道修复是不是硬盘东西全部都没有了_硬盘有坏道就不能用了吗?别再吃哑巴亏了,今天跟大家再说一次...
  6. 远程网络安装RHEL5
  7. jboss:在standalone.xml中设置系统属性(system-properties)
  8. Delphi FireMonkey 的动画以及多线程
  9. vkt中使用OBJImporter导入模型并进行贴图
  10. GSL-蒙特卡洛积分
  11. Reasoning methods include
  12. 植物大战僵尸修改存档总结
  13. Kali Linux——小白安装过程(2021)
  14. MATLAB数组相除
  15. 实用工具推荐--GIF动图裁剪工具IIMG(免注册且免费)
  16. Unable to negotiate with 192.168.2.53 port 22: no matching host key type found. Their offer: ssh-rsa
  17. 微信小程序判断手机系统
  18. android原生和H5交互(转)
  19. spring boot 启动报错:No qualifying bean of type org.springframework.security.oauth2.provider.token.Toke
  20. 黑河学院2021书法高考成绩查询,2021年黑河学院高考录取结果什么时候出来及查询系统入口...

热门文章

  1. [原创]商品条码管理与条码打印管理软件的实现
  2. 为链接加上加载等待信息
  3. session理论分析
  4. 2.maven 安装配置
  5. vue样式控制的方式
  6. Python全栈开发——subprocess struct
  7. vue2.0 element学习
  8. WebSocket 协议
  9. iOS开发 字符串的转化 小技巧
  10. java webservice