• [Swift]LeetCode1293. 网格中的最短路径 | Shortest Path in a Grid with Obstacles Elimination


    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝(let_us_code)
    ➤博主域名:https://www.zengqiang.org
    ➤GitHub地址:https://github.com/strengthen/LeetCode
    ➤原文地址:https://www.cnblogs.com/strengthen/p/12151604.html
    ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
    ➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

    Given a m * n grid, where each cell is either 0 (empty) or 1 (obstacle). In one step, you can move up, down, left or right from and to an empty cell.

    Return the minimum number of steps to walk from the upper left corner (0, 0) to the lower right corner (m-1, n-1) given that you can eliminate at most k obstacles. If it is not possible to find such walk return -1.

    Example 1:

    Input:
    grid =
    [[0,0,0],
     [1,1,0],
    [0,0,0],
     [0,1,1],
    [0,0,0]],
    k = 1
    Output: 6
    Explanation:
    The shortest path without eliminating any obstacle is 10. 
    The shortest path with one obstacle elimination at position (3,2) is 6. Such path is (0,0) -> (0,1) -> (0,2) -> (1,2) -> (2,2) -> (3,2) -> (4,2).
     

    Example 2:

    Input:
    grid =
    [[0,1,1],
     [1,1,1],
     [1,0,0]],
    k = 1
    Output: -1
    Explanation:
    We need to eliminate at least two obstacles to find such a walk.
     

    Constraints:

    grid.length == m
    grid[0].length == n
    1 <= m, n <= 40
    1 <= k <= m*n
    grid[i][j] == 0 or 1
    grid[0][0] == grid[m-1][n-1] == 0


    给你一个 m * n 的网格,其中每个单元格不是 0(空)就是 1(障碍物)。每一步,您都可以在空白单元格中上、下、左、右移动。

    如果您 最多 可以消除 k 个障碍物,请找出从左上角 (0, 0) 到右下角 (m-1, n-1) 的最短路径,并返回通过该路径所需的步数。如果找不到这样的路径,则返回 -1。

    示例 1:

    输入:
    grid =
    [[0,0,0],
     [1,1,0],
    [0,0,0],
     [0,1,1],
    [0,0,0]],
    k = 1
    输出:6
    解释:
    不消除任何障碍的最短路径是 10。
    消除位置 (3,2) 处的障碍后,最短路径是 6 。该路径是 (0,0) -> (0,1) -> (0,2) -> (1,2) -> (2,2) -> (3,2) -> (4,2).
     

    示例 2:

    输入:
    grid =
    [[0,1,1],
     [1,1,1],
     [1,0,0]],
    k = 1
    输出:-1
    解释:
    我们至少需要消除两个障碍才能找到这样的路径。
     

    提示:

    grid.length == m
    grid[0].length == n
    1 <= m, n <= 40
    1 <= k <= m*n
    grid[i][j] == 0 or 1
    grid[0][0] == grid[m-1][n-1] == 0

  • 相关阅读:
    过用户层HOOK思路
    Linux LVM实践
    matlab演奏卡农 Cripple Pachebel's Canon on Matlab
    rman备份恢复总结
    郁金香VC外挂教程(全) 翻录版 免Key(精品教程)
    C# string 中的 @ 作用处理\等字符
    (抓)2分法通用存储过程分页(top max模式)版本(性能相对之前的not in版本极大提高)
    怎样应用OracleParameter怎样写like查询语句?
    (转)DirectoryEntry的使用
    解决模式对话框和window.open打开新页面Session会丢失问题
  • 原文地址:https://www.cnblogs.com/strengthen/p/12151604.html
Copyright © 2020-2023  润新知