1631. 最小体力消耗路径
链接: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. 最小体力消耗路径相关推荐
- leetcode 1631. 最小体力消耗路径(并查集)
你准备参加一场远足活动.给你一个二维 rows x columns 的地图 heights ,其中 heights[row][col] 表示格子 (row, col) 的高度.一开始你在最左上角的格子 ...
- LeetCode 1631. 最小体力消耗路径(DFS + 二分查找)
文章目录 1. 题目 2. 解题 1. 题目 你准备参加一场远足活动.给你一个二维 rows x columns 的地图 heights ,其中 heights[row][col] 表示格子 (row ...
- 【LeetCode】1631. Path With Minimum Effort 最小体力消耗路径(Medium)(JAVA)每日一题
[LeetCode]1631. Path With Minimum Effort 最小体力消耗路径(Medium)(JAVA) 题目描述: You are a hiker preparing for ...
- 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 ...
- 有n堆石子,每次取出两堆合成一堆,每堆石子的个数即为合并石子所需要耗费的体力,求出合并所有石子堆所需要耗费的最小体力
有n堆石子,每次取出两堆合成一堆,每堆石子的个数即为合并石子所需要耗费的体力,求出合并所有石子堆所需要耗费的最小体力 典型的贪心题,即每次取出数量最少的两堆石子合并. 举个例子来说,假如有5堆石子,石 ...
- hdu 5092 线裁剪(纵向连线最小和+输出路径)
http://acm.hdu.edu.cn/showproblem.php?pid=5092 给一个m*n的矩阵,找到一个纵向的"线"使得线上的和最小并输出这条线,线能向8个方向延 ...
- 三角形最小/最大路径
题目描述: 给定一个三角形,找出自顶向下的最小路径和.每一步只能移动到下一行中相邻的结点上. 例如,给定三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] 自顶向下的最小路 ...
- leetcode 931. Minimum Falling Path Sum(最小的下降路径和)
下降的路径就是从第一行任一个元素出发,只能直着或斜着走到下一行,直到到达最后一行,返回形成的路径中最小的路径和. 思路: DP 从下往上推, 在最后一行选一个元素,那么它是从哪里过来的?是从上一行的j ...
- 最短路径(Shortest Paths)
最短路径 最短路径(Shortest Paths) 最短路径问题一直是图论研究的热点问题.例如在实际生活中的路径规划.地图导航等领域有重要的应用.关于求解图的最短路径方法也层出不穷,本篇文章将详细讲解 ...
最新文章
- 凭证 90000000 保存(帐户确定出错)
- 推荐算法工程师成长2:排序模块
- Java 源码赏析 - java.lang - Void
- Intel PAUSE指令变化影响到MySQL的性能,该如何解决?
- 坏道修复是不是硬盘东西全部都没有了_硬盘有坏道就不能用了吗?别再吃哑巴亏了,今天跟大家再说一次...
- 远程网络安装RHEL5
- jboss:在standalone.xml中设置系统属性(system-properties)
- Delphi FireMonkey 的动画以及多线程
- vkt中使用OBJImporter导入模型并进行贴图
- GSL-蒙特卡洛积分
- Reasoning methods include
- 植物大战僵尸修改存档总结
- Kali Linux——小白安装过程(2021)
- MATLAB数组相除
- 实用工具推荐--GIF动图裁剪工具IIMG(免注册且免费)
- Unable to negotiate with 192.168.2.53 port 22: no matching host key type found. Their offer: ssh-rsa
- 微信小程序判断手机系统
- android原生和H5交互(转)
- spring boot 启动报错:No qualifying bean of type org.springframework.security.oauth2.provider.token.Toke
- 黑河学院2021书法高考成绩查询,2021年黑河学院高考录取结果什么时候出来及查询系统入口...