leetcode - 1293. Shortest Path in a Grid with Obstacles Elimination

这篇具有很好参考价值的文章主要介绍了leetcode - 1293. Shortest Path in a Grid with Obstacles Elimination。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Description

You are given an m x n integer matrix grid where each cell is either 0 (empty) or 1 (obstacle). You can move up, down, left, or right from and to an empty cell in one step.

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:
leetcode - 1293. Shortest Path in a Grid with Obstacles Elimination,OJ题目记录,leetcode,算法,职场和发展

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:
leetcode - 1293. Shortest Path in a Grid with Obstacles Elimination,OJ题目记录,leetcode,算法,职场和发展

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:

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

Solution

BFS, but use the remaining time for removing rocks as the visited label. It’s always better if we reach the same point, and the remaining chance to remove rocks is larger.

Time complexity: o ( m ∗ n ) o(m * n) o(mn)
Space complexity: o ( m ∗ n ) o(m * n) o(mn)文章来源地址https://www.toymoban.com/news/detail-719526.html

Code

class Solution:
    def shortestPath(self, grid: List[List[int]], k: int) -> int:
        queue = collections.deque([(0, 0, k, 0)])
        visited = {}
        m, n = len(grid), len(grid[0])
        while queue:
            x, y, r, step = queue.popleft()
            if visited.get((x, y), -1) >= r:
                continue
            if x == m - 1 and y == n - 1:
                return step
            visited[(x, y)] = r
            for dz in (-1, 1):
                if 0 <= x + dz < m:
                    if grid[x + dz][y] == 0:
                        queue.append((x + dz, y, r, step + 1))
                    elif r - 1 >= 0:
                        queue.append((x + dz, y, r - 1, step + 1))
                if 0 <= y + dz < n:
                    if grid[x][y + dz] == 0:
                        queue.append((x, y + dz, r, step + 1))
                    elif r - 1 >= 0:
                        queue.append((x, y + dz, r - 1, step + 1))
        return -1

到了这里,关于leetcode - 1293. Shortest Path in a Grid with Obstacles Elimination的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Error creating bean with name ‘kafkaTemplate‘ defined in class path resource

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name \\\'org.springframework.boot.autoconfigure.kafka.KafkaAnnotationDrivenConfiguration\\\': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.autoconfigure.kafka.Kafka

    2024年02月07日
    浏览(58)
  • 报错 Error creating bean with name ‘elasticsearchTemplate‘ defined in class path resource

    报错信息如下: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name \\\'indexController\\\': Unsatisfied dependency expressed through field \\\'articleService\\\'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name \\\'articleServiceImpl\\\': Unsatisfied dependen

    2023年04月08日
    浏览(45)
  • 解决Error creating bean with name ‘sqlSessionFactory‘ defined in class path resource

    Error creating bean with name \\\'sqlSessionFactory\\\' defined in class path resource 出错背景:项目中使用mybatisplus开发,涉及到了xml文件,现在需要下线个功能,所以就先把相关的代码注释掉了,但是在启动的时候不知道为什么会报这个错:Error creating bean with name \\\'sqlSessionFactory\\\' defined in class pat

    2024年02月11日
    浏览(45)
  • 错误解决:Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception......

    目录 前言: 目的需求: 源代码: 报错信息: 错误解决:  总结:        这里出错的原因与大多数人并不相同,这里仅为个人记录。        作为一个菜只因,总是能深刻体会到一个bug改一天或者几天的痛苦......在做spring项目时,需要利用session保存用户信息,启动项目登

    2024年02月06日
    浏览(44)
  • Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception (已解决)

    后端:springboot mybatis 异常: 1.controller层没有加@ResponseBody 2.Service层实现类未添加注解@Autowired 3.@RestController使用成了@Controller 全网基本上都是这种解决方案,这种解决方案其他博主说有详细说明 这种错误如果都试过了还是报异常,这个也是我经常犯的错误 那必然是jar包多导了

    2023年04月14日
    浏览(34)
  • 异常报错:Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception

    问题描述 异常信息: 第一类错误 Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause 第二类错误 Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing fai

    2023年04月19日
    浏览(41)
  • 已解决异常:Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception

    已解决异常:Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception 今天开发的时候,遇到了这个bug: “dispatcherServlet” 的异常问题。 详细报错如下: 起初还以为是 SpringBoot 底层框架的问题,毕竟涉及到了 DispatcherServlet 。 但是仔细看了之后发现问题不是这么

    2024年02月10日
    浏览(40)
  • LeetCode --- 1971. Find if Path Exists in Graph 解题报告

    There is a  bi-directional  graph with  n  vertices, where each vertex is labeled from  0  to  n - 1  ( inclusive ). The edges in the graph are represented as a 2D integer array  edges , where each  edges[i] = [ui, vi]  denotes a bi-directional edge between vertex  ui  and vertex  vi . Every vertex pair is connected by  at most one  edge, and

    2024年02月07日
    浏览(44)
  • Error creating bean with name ‘sqlSessionFactory‘ defined in class path resource [application-config

    学习spring框架时遇到一个问题。 记录一个错误:   摘出最主要的提示: 主要问题Error creating bean with name \\\'sqlSessionFactory\\\' defined in class path resource [applicationContext.xml]: Invocation of init method failed;在创建bean时,调用初始化方法失败。从而引出后面集中嵌套的错误。 定位到最终代码

    2024年02月06日
    浏览(36)
  • Error creating bean with name ‘s‘ defined in class path resource [applicationContext.xml]: Initiali.

    在用AOP写小项目的时候出现了这个问题 Error creating bean with name \\\'s\\\' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is java.lang.ExceptionInInitializerError 翻译过来大概的意思就是  创建在类路径资源[applicationContext.xml]中定义的名为“s”的bean时出错:

    2024年02月11日
    浏览(43)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包