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) 。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值