LeetCode Contest 178-1368. 使网格图至少有一条有效路径的最小代价 Minimum Cost to Make at Least One Valid Path in a Grid

简介: LeetCode Contest 178-1368. 使网格图至少有一条有效路径的最小代价 Minimum Cost to Make at Least One Valid Path in a Grid

LeetCode Contest 178-1368. 使网格图至少有一条有效路径的最小代价 Minimum Cost to Make at Least One Valid Path in a Grid


目录

一、中文版

二、英文版

三、My answer

四、解题报告

一、中文版

给你一个 m x n 的网格图 grid 。 grid 中每个格子都有一个数字,对应着从该格子出发下一步走的方向。 grid[i][j] 中的数字可能为以下几种情况:

1 ,下一步往右走,也就是你会从 grid[i][j] 走到 grid[i][j + 1]

2 ,下一步往左走,也就是你会从 grid[i][j] 走到 grid[i][j - 1]

3 ,下一步往下走,也就是你会从 grid[i][j] 走到 grid[i + 1][j]

4 ,下一步往上走,也就是你会从 grid[i][j] 走到 grid[i - 1][j]

注意网格图中可能会有 无效数字 ,因为它们可能指向 grid 以外的区域。

一开始,你会从最左上角的格子 (0,0) 出发。我们定义一条 有效路径 为从格子 (0,0) 出发,每一步都顺着数字对应方向走,最终在最右下角的格子 (m - 1, n - 1) 结束的路径。有效路径 不需要是最短路径 。

你可以花费 cost = 1 的代价修改一个格子中的数字,但每个格子中的数字 只能修改一次 。

请你返回让网格图至少有一条有效路径的最小代价。

示例 1:

网络异常,图片无法展示
|

输入:grid = [[1,1,1,1],[2,2,2,2],[1,1,1,1],[2,2,2,2]]

输出:3

解释:你将从点 (0, 0) 出发。

到达 (3, 3) 的路径为: (0, 0) --> (0, 1) --> (0, 2) --> (0, 3) 花费代价 cost = 1 使方向向下 --> (1, 3) --> (1, 2) --> (1, 1) --> (1, 0) 花费代价 cost = 1 使方向向下 --> (2, 0) --> (2, 1) --> (2, 2) --> (2, 3) 花费代价 cost = 1 使方向向下 --> (3, 3)

总花费为 cost = 3.

示例 2:

网络异常,图片无法展示
|

输入:grid = [[1,1,3],[3,2,2],[1,1,4]]

输出:0

解释:不修改任何数字你就可以从 (0, 0) 到达 (2, 2) 。

示例 3:

输入:grid = [[1,2],[4,3]]

输出:1

示例 4:

输入:grid = [[2,2,2],[2,2,2]]

输出:3

示例 5:

输入:grid = [[4]]

输出:0

 

提示:

m == grid.length

n == grid[i].length

1 <= m, n <= 100

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/minimum-cost-to-make-at-least-one-valid-path-in-a-grid

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

二、英文版

Given a m x n grid. Each cell of the grid has a sign pointing to the next cell you should visit if you are currently in this cell. The sign of grid[i][j] can be:
1 which means go to the cell to the right. (i.e go from grid[i][j] to grid[i][j + 1])
2 which means go to the cell to the left. (i.e go from grid[i][j] to grid[i][j - 1])
3 which means go to the lower cell. (i.e go from grid[i][j] to grid[i + 1][j])
4 which means go to the upper cell. (i.e go from grid[i][j] to grid[i - 1][j])
Notice that there could be some invalid signs on the cells of the grid which points outside the grid.
You will initially start at the upper left cell (0,0). A valid path in the grid is a path which starts from the upper left cell (0,0) and ends at the bottom-right cell (m - 1, n - 1) following the signs on the grid. The valid path doesn't have to be the shortest.
You can modify the sign on a cell with cost = 1. You can modify the sign on a cell one time only.
Return the minimum cost to make the grid have at least one valid path.

Example 1:

网络异常,图片无法展示
|

Input: grid = [[1,1,1,1],[2,2,2,2],[1,1,1,1],[2,2,2,2]]
Output: 3
Explanation: You will start at point (0, 0).
The path to (3, 3) is as follows. (0, 0) --> (0, 1) --> (0, 2) --> (0, 3) change the arrow to down with cost = 1 --> (1, 3) --> (1, 2) --> (1, 1) --> (1, 0) change the arrow to down with cost = 1 --> (2, 0) --> (2, 1) --> (2, 2) --> (2, 3) change the arrow to down with cost = 1 --> (3, 3)
The total cost = 3.

Example 2:

网络异常,图片无法展示
|

Input: grid = [[1,1,3],[3,2,2],[1,1,4]]
Output: 0
Explanation: You can follow the path from (0, 0) to (2, 2).

Example 3:

网络异常,图片无法展示
|

Input: grid = [[1,2],[4,3]]
Output: 1
Example 4:
Input: grid = [[2,2,2],[2,2,2]]
Output: 3
Example 5:
Input: grid = [[4]]
Output: 0
 
Constraints:
m == grid.length
n == grid[i].length
1 <= m, n <= 100
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/minimum-cost-to-make-at-least-one-valid-path-in-a-grid
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

三、My answer

class Solution:
    # 按移动花费来 bfs 遍历图
    def minCost(self, grid: List[List[int]]) -> int:
        m = len(grid)
        n = len(grid[0])
        # 移动方向数组,index+1 = 题目里的编号
        dirs = [(0,1), (0,-1),(1,0),(-1,0)]
        visited = set()
        # 为了简单一点没有用队列,使用了dict+list,key 为 cost,value 为下标组成的list
        # 表示从起始点到该list的中的点的花费为key值
        costs = {0:[(0,0)]} 
        cost = 0
        # 按 cost 遍历,由于遍历是 costs 中的 cost 会增多,不写成 for cost in costs
        while cost in costs:
            for i,j in costs[cost]:
                # 目标点没有访问过
                if (i,j) not in visited:
                    visited.add((i,j))
                    # 到达终点结束
                    if i == m-1 and j == n-1:
                        return cost
                    # 求四个方向的消耗,并加入costs
                    for val, d in enumerate(dirs):
                        x = i + d[0]
                        y = j + d[1]
                        # 目标点合法,且没有访问过
                        if 0<=x<m and 0<=j<n and (x,y) not in visited:
                            # 如果和i,j的方向一致则cost不变,否则加1
                            next_cost = cost + (0 if val+1 == grid[i][j] else 1)
                            # 加入 costs
                            if next_cost not in costs:
                                costs[next_cost] = []
                            costs[next_cost].append((x,y))
            cost += 1
        return 0

四、解题报告

看到题目考虑bfs遍历图,可以从起始点出发,先走遍能直接连同的点。这些点相邻的点的cost就是1,再走遍所有cost为1点,他们相邻的点就是cost为2的点。以此类推,一直到终点,就返回其cost。如果一个点有多个cost,当然是取其中的最小值,实际表现出来就是已经走过的点不用再走一边了。

时间复杂的 O(m*n) 即 每一个点遍历一次;空间复杂度O(m*n) 。

相关文章
|
9月前
【LeetCode 35】112.路径总和
【LeetCode 35】112.路径总和
73 0
|
算法 Unix 测试技术
力扣经典150题第五十二题:简化路径
力扣经典150题第五十二题:简化路径
87 0
|
2月前
|
Go 开发者 索引
【LeetCode 热题100】路径与祖先:二叉树中的深度追踪技巧(力扣33 / 81/ 153/154)(Go语言版)
本文深入探讨了LeetCode中四道关于「搜索旋转排序数组」的经典题目,涵盖了无重复和有重复元素的情况。通过二分查找的变形应用,文章详细解析了每道题的解题思路和Go语言实现代码。关键点包括判断有序区间、处理重复元素以及如何缩小搜索范围。文章还总结了各题的异同,并推荐了类似题目,帮助读者全面掌握二分查找在旋转数组中的应用。无论是初学者还是有经验的开发者,都能从中获得实用的解题技巧和代码实现方法。
200 14
|
3月前
|
Go
【LeetCode 热题100】路径与祖先:二叉树中的深度追踪技巧(力扣437 / 236 )(Go语言版)
本文深入探讨二叉树中路径与祖先问题,涵盖两道经典题目:LeetCode 437(路径总和 III)和236(最近公共祖先)。对于路径总和 III,文章分析了双递归暴力解法与前缀和优化方法,后者通过哈希表记录路径和,将时间复杂度从O(n²)降至O(n)。在最近公共祖先问题中,采用后序遍历递归查找,利用“自底向上”的思路确定最近公共祖先节点。文中详细解析代码实现与核心要点,帮助读者掌握深度追踪技巧,理解树结构中路径与节点关系的本质。这类问题在面试中高频出现,掌握其解法意义重大。
77 4
|
3月前
|
算法 Go
【LeetCode 热题100】深入理解二叉树结构变化与路径特性(力扣104 / 226 / 114 / 543)(Go语言版)
本博客深入探讨二叉树的深度计算、结构变换与路径分析,涵盖四道经典题目:104(最大深度)、226(翻转二叉树)、114(展开为链表)和543(二叉树直径)。通过递归与遍历策略(前序、后序等),解析每题的核心思路与实现方法。结合代码示例(Go语言),帮助读者掌握二叉树相关算法的精髓。下一讲将聚焦二叉树构造问题,欢迎持续关注!
83 10
|
9月前
【LeetCode 36】113.路径总和II
【LeetCode 36】113.路径总和II
69 0
|
11月前
|
机器人 Python
【Leetcode刷题Python】62. 不同路径
LeetCode 62题 "不同路径" 的Python解决方案,使用动态规划算法计算机器人从网格左上角到右下角的所有可能路径数量。
158 0
|
11月前
|
Python
【Leetcode刷题Python】113. 路径总和 II
LeetCode上113号问题"路径总和 II"的Python实现,通过深度优先搜索来找出所有从根节点到叶子节点路径总和等于给定目标和的路径。
75 3
【Leetcode刷题Python】113. 路径总和 II
|
11月前
|
存储 Python
【Leetcode刷题Python】64. 最小路径和
一种使用动态规划解决LeetCode上64题“最小路径和”的Python实现方法,通过维护一个一维数组来计算从网格左上角到右下角的最小路径总和。
57 1
【Leetcode刷题Python】64. 最小路径和
|
9月前
【LeetCode 34】257.二叉树的所有路径
【LeetCode 34】257.二叉树的所有路径
68 0

热门文章

最新文章