当前位置: 首页 > news >正文

微站和网站数据广州越秀区怎么样

微站和网站数据,广州越秀区怎么样,四川省住房建设厅网站,务川网站建设【LetMeFly】1631.最小体力消耗路径#xff1a;广度优先搜索BFS 力扣题目链接#xff1a;https://leetcode.cn/problems/path-with-minimum-effort/ 你准备参加一场远足活动。给你一个二维 rows x columns 的地图 heights #xff0c;其中 heights[row][col] 表示格子 (ro…【LetMeFly】1631.最小体力消耗路径广度优先搜索BFS 力扣题目链接https://leetcode.cn/problems/path-with-minimum-effort/ 你准备参加一场远足活动。给你一个二维 rows x columns 的地图 heights 其中 heights[row][col] 表示格子 (row, col) 的高度。一开始你在最左上角的格子 (0, 0) 且你希望去最右下角的格子 (rows-1, columns-1) 注意下标从 0 开始编号。你每次可以往 上下左右 四个方向之一移动你想要找到耗费 体力 最小的一条路径。 一条路径耗费的 体力值 是路径上相邻格子之间 高度差绝对值 的 最大值 决定的。 请你返回从左上角走到右下角的最小 体力消耗值 。 示例 1 输入heights [[1,2,2],[3,8,2],[5,3,5]] 输出2 解释路径 [1,3,5,3,5] 连续格子的差值绝对值最大为 2 。 这条路径比路径 [1,2,2,2,5] 更优因为另一条路径差值最大值为 3 。示例 2 输入heights [[1,2,3],[3,8,4],[5,3,5]] 输出1 解释路径 [1,2,3,4,5] 的相邻格子差值绝对值最大为 1 比路径 [1,3,5,3,5] 更优。示例 3 输入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.lengthcolumns heights[i].length1 rows, columns 1001 heights[i][j] 106 方法一广度优先搜索BFS 首先我们可以构造一个图图中顶点是矩阵中的点图中的边是矩阵中相邻点的绝对值之差。 这样我们只需要从起点0开始使用一个优先队列进行广度优先搜索。每次找出未遍历的点中与已遍历的点中绝对值之差最小的点。入队时将“点的位置”和“从起点到该点的最大绝对值之差”入队。 最终返回最后一个位置距离起点的最大绝对值之差即为答案。 时间复杂度 O ( n m log ⁡ n m ) O(nm\log nm) O(nmlognm)其中 s i z e ( g r a p h ) n × m size(graph)n\times m size(graph)n×m空间复杂度 O ( n m ) O(nm) O(nm) AC代码 C const int directions[4][2] {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};class Solution { public:int minimumEffortPath(vectorvectorint heights) {int n heights.size(), m heights[0].size();vectorvectorpairint, int graph(n * m); // graph[i]: [[j, 5], [k, 3]]for (int i 0; i n; i) {for (int j 0; j m; j) {for (int d 0; d 4; d) {int ti i directions[d][0], tj j directions[d][1];if (ti 0 || ti n || tj 0 || tj m) {continue;}int diff abs(heights[i][j] - heights[ti][tj]);int x i * m j, y ti * m tj;graph[x].push_back({y, diff});}}}auto cmp [](const pairint, int a, const pairint, int b) {return a.second b.second;};priority_queuepairint, int, vectorpairint, int, decltype(cmp) pq(cmp);pq.push({0, 0});vectorint ans(n * m, 1e9); // 从0到i的最大绝对值之差ans[0] 0;while (pq.size()) {auto [index, diff] pq.top();pq.pop();for (auto [toIndex, toDiff] : graph[index]) {int nextDiff max(diff, toDiff);if (ans[toIndex] nextDiff) {ans[toIndex] nextDiff;pq.push({toIndex, nextDiff});}}}return ans.back();} };Python # from typing import List # import heapqdirections [[-1, 0], [1, 0], [0, -1], [0, 1]]class Solution:def minimumEffortPath(self, heights: List[List[int]]) - int:n, m len(heights), len(heights[0])graph [[] for _ in range(n * m)]for i in range(n):for j in range(m):for di, dj in directions:ti, tj i di, j djif ti 0 or ti n or tj 0 or tj m:continuegraph[i * m j].append((ti * m tj, abs(heights[ti][tj] - heights[i][j])))pq [(0, 0)]ans [1000000000] * (n * m)ans[0] 0while pq:thisDiff, thisNode heapq.heappop(pq)for toNode, toDiff in graph[thisNode]:newDiff max(thisDiff, toDiff)if ans[toNode] newDiff:ans[toNode] newDiffheapq.heappush(pq, (newDiff, toNode))# print(thisNode, toNode, newDiff)return ans[-1]同步发文于CSDN原创不易转载经作者同意后请附上原文链接哦~ Tisfyhttps://letmefly.blog.csdn.net/article/details/134934684
http://wiki.neutronadmin.com/news/25986/

相关文章:

  • 长沙网站seo收费标准wordpress怎么弄中文
  • 做牛津纺衬衫的网站免费企业网站php源码
  • 地方门户网站模版网站开发方案怎么写
  • 设计网站汇总wordpress与typecho
  • 承德网站建设报价小程序微盟
  • 优美网站源码前端做网站都要做哪些
  • 深圳线运营是网站建设推网怎么制作
  • 常州个人做网站河南工程学院网站建设
  • 网站不想被百度抓取asp网站开发 pdf
  • 唐山乾正建设工程材料检测公司网站哈尔滨网站建设费用
  • 中文域名.网站泉州手机端建站模板
  • 如何找到做网站的客户浙江网站备案流程
  • html后缀的网站运动健身类网站开发
  • 上海网站建设的价格是多少钱青海小学网站建设
  • vue 做电商网站宁波网页设计找哪家
  • 大连seo推广优化seo推广是什么
  • 最大的搜索网站排名软件如何制作
  • 自媒体时代做网站有前途吗做视频网站
  • 诸暨制作网站的公司有哪些你不会百度吗网页生成
  • 做网站配置好了找不到服务器技术支持 东莞网站建设电脑回收
  • 免费网站源码下载平台wordpress里的发消息给我
  • 佛山企业如何建网站公司网站首页布局图
  • 怎么做培训班网站网站开发实战
  • dede做招聘网站深圳我的网站
  • 网站设计的内容python wordpress建站
  • 模版网站建设企业百度seo优化网站
  • 手机微网站第二年续费吗旅游找什么网站好
  • 建设搜索引擎网站江苏省建是国企吗
  • 网站建设团队分工贵安新区网站建设推广
  • 做网站域名要自己注册吗个体工商网站备案