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

leetcode-1210. Minimum Moves to Reach Target with Rotations

题目

In an n*n grid, there is a snake that spans 2 cells and starts moving from the top left corner at (0, 0) and (0, 1). The grid has empty cells represented by zeros and blocked cells represented by ones. The snake wants to reach the lower right corner at (n-1, n-2) and (n-1, n-1).

In one move the snake can:

Move one cell to the right if there are no blocked cells there. This move keeps the horizontal/vertical position of the snake as it is.
Move down one cell if there are no blocked cells there. This move keeps the horizontal/vertical position of the snake as it is.
Rotate clockwise if it’s in a horizontal position and the two cells under it are both empty. In that case the snake moves from (r, c) and (r, c+1) to (r, c) and (r+1, c).
在这里插入图片描述

Rotate counterclockwise if it’s in a vertical position and the two cells to its right are both empty. In that case the snake moves from (r, c) and (r+1, c) to (r, c) and (r, c+1).
在这里插入图片描述

Return the minimum number of moves to reach the target.

If there is no way to reach the target, return -1.

Example 1:

Input: grid = [[0,0,0,0,0,1],
               [1,1,0,0,1,0],
               [0,0,0,0,1,1],
               [0,0,1,0,1,0],
               [0,1,1,0,0,0],
               [0,1,1,0,0,0]]
Output: 11
Explanation:
One possible solution is [right, right, rotate clockwise, right, down, down, down, down, rotate counterclockwise, right, down].

Example 2:

Input: grid = [[0,0,1,1,1,1],
               [0,0,0,0,1,1],
               [1,1,0,0,0,1],
               [1,1,1,0,0,1],
               [1,1,1,0,0,1],
               [1,1,1,0,0,0]]
Output: 9

Constraints:

2 <= n <= 100
0 <= grid[i][j] <= 1
It is guaranteed that the snake starts at empty cells.

解题思路

图的bfs求最短路径,注意旋转有前提即可

时间、空间复杂度 o ( n 2 ) o(n^2) o(n2)

WA
case39一直过不了,我也没明白是为什么。。。肉眼看是有解的呀,不知道为什么正确答案是-1。。。

在这里插入图片描述

代码

class Solution:
    def minimumMoves(self, grid: List[List[int]]) -> int:

        def is_valid(grid, node1, node2) -> bool:
            n = len(grid)
            return 0 <= node1[0] < n and 0 <= node1[1] < n and 0 <= node2[0] < n and 0 <= node2[1] < n and grid[node1[0]][node1[1]] == 0 and grid[node2[0]][node2[1]] == 0

        import collections
        n = len(grid)
        queue = collections.deque([((0, 0), (0, 1), 0)])
        visited_spots = set()
        min_moves = -1
        while queue:
            node1, node2, path = queue.popleft()
            if node1 == (n - 1, n - 2) and node2 == (n - 1, n - 1):
                min_moves = min(min_moves if min_moves != -1 else 10000000, path)
            if (node1, node2) in visited_spots:
                continue
            visited_spots.add((node1, node2))
            # right
            new_node1, new_node2 = (node1[0], node1[1] + 1), (node2[0], node2[1] + 1)
            if is_valid(grid, new_node1, new_node2):
                queue.append((new_node1, new_node2, path + 1))
            # down
            new_node1, new_node2 = (node1[0] + 1, node1[1]), (node2[0] + 1, node2[1])
            if is_valid(grid, new_node1, new_node2):
                queue.append((new_node1, new_node2, path + 1))
            # rotate clockwise
            if node2[1] == node1[1] + 1:
                new_node1, new_node2 = (node1[0], node1[1]), (node2[0] + 1, node2[1] - 1)
                if is_valid(grid, new_node1, new_node2):
                    queue.append((new_node1, new_node2, path + 1))
            # rotate counterclockwise
            if node2[0] == node1[0] + 1:
                new_node1, new_node2 = (node1[0], node1[1]), (node2[0] - 1, node2[1] + 1)
                if is_valid(grid, new_node1, new_node2):
                    queue.append((new_node1, new_node2, path + 1))
        return min_moves

相关文章:

  • 基于linux5.15.5的IMX 参考手册 --- 19
  • python入门 之 列表(四)
  • 【微服务】微服务常见面试题
  • MySQL 解析binlog生成标准SQL工具之my2sql
  • 《从0开始学大数据》之知名大厂大数据平台介绍
  • 锐捷(十二)MPLS VXN基础配置实验
  • wpas 关联流程分析
  • MongoDB 正则表达式
  • 微信小程序模块化、组件传值、添加data,menthods类型等记录-持续更新
  • Spring常用注解
  • 图论(8)LCA
  • 【设计模式】命令模式(Command Pattren)
  • Flutter局部刷新解决闪烁问题
  • hive的数据库和表操作
  • 力扣第331场周赛题解
  • 【Java】Spring Cloud 教程
  • 44 计算概论与程序设计基础21h-北京大学
  • Android-Native开发系列之利用AAudio播放音频
  • 道路病害识别监测系统 CNN网络
  • 基于wxbot的微信聊天机器人
  • 电加热油锅炉工作原理_电加热导油
  • 大型电蒸汽锅炉_工业电阻炉
  • 燃气蒸汽锅炉的分类_大连生物质蒸汽锅炉
  • 天津市维修锅炉_锅炉汽化处理方法
  • 蒸汽汽锅炉厂家_延安锅炉厂家
  • 山西热水锅炉厂家_酒店热水 锅炉
  • 蒸汽锅炉生产厂家_燃油蒸汽发生器
  • 燃煤锅炉烧热水_张家口 淘汰取缔燃煤锅炉
  • 生物质锅炉_炉
  • 锅炉天然气_天燃气热风炉