题目如下:

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题目。特别要注意的是蛇在水平/垂直方向是可以平移的。比如当前所在的左边是(0,0)(0,1),可以平移到(1,0),(1,1)。

代码如下:

class Solution(object):
def minimumMoves(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
res = float('inf')
queue = [(0,0,0,1,0)]
dic = {}
dic[(0,0,0,1)] = 0
while len(queue) > 0:
tx,ty,hx,hy,count = queue.pop(0)
#print tx,ty,hx,hy,count
if hx == len(grid) - 1 == hy and tx == len(grid)-1 and ty == len(grid) - 2:
res = min(res,count)
continue
if tx == hx and ty < hy: #head to right
if hy + 1 < len(grid) and grid[hx][hy+1] == 0:
key = (tx,ty+1,hx,hy+1)
if key not in dic or dic[key] > count + 1:
queue.append((tx,ty+1,hx,hy+1,count+1))
dic[key] = count + 1
if hx + 1 < len(grid) and grid[tx+1][ty] == 0 and grid[hx+1][hy] == 0:
key = (tx, ty, hx+1, ty)
if key not in dic or dic[key] > count + 1:
queue.append((tx, ty, hx+1, ty, count + 1))
dic[key] = count + 1
key = (tx+1,ty,hx+1,hy)
if key not in dic or dic[key] > count + 1:
queue.append((tx+1,ty,hx+1,hy, count + 1))
dic[key] = count + 1
elif tx < hx and ty == hy: #head to down
if hx + 1 < len(grid) and grid[hx+1][hy] == 0:
key = (tx+1,ty,hx+1,hy)
if key not in dic or dic[key] > count + 1:
queue.append((tx+1,ty,hx+1,hy,count+1))
dic[key] = count + 1
if hy + 1 < len(grid) and grid[hx][hy+1] == 0 and grid[tx][ty+1] == 0:
key = tx,ty,tx,ty+1
if key not in dic or dic[key] > count + 1:
queue.append((tx,ty,tx,ty+1,count+1))
dic[key] = count + 1
key = tx, ty+1, tx, ty + 1
if key not in dic or dic[key] > count + 1:
queue.append((tx,ty+1,hx,hy+1,count+1))
dic[key] = count + 1
return res if res != float('inf') else -1

【leetcode】1210. Minimum Moves to Reach Target with Rotations的更多相关文章

  1. 【leetcode】453. Minimum Moves to Equal Array Elements

    problem 453. Minimum Moves to Equal Array Elements 相当于把不等于最小值的数字都减到最小值所需要次数的累加和. solution1: class So ...

  2. 【LeetCode】453. Minimum Moves to Equal Array Elements 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:模拟过程 方法二:求和-n*最小值 方法三: ...

  3. 【LeetCode】462. Minimum Moves to Equal Array Elements II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:排序 方法二:直接找中位数 日期 题目地址: ...

  4. 【LeetCode】462. Minimum Moves to Equal Array Elements II

    Given a non-empty integer array, find the minimum number of moves required to make all array element ...

  5. 【leetcode】963. Minimum Area Rectangle II

    题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...

  6. 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)

    [LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...

  7. 【leetcode】712. Minimum ASCII Delete Sum for Two Strings

    题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...

  8. 【LeetCode】Find Minimum in Rotated Sorted Array 解题报告

    今天看到LeetCode OJ题目下方多了"Show Tags"功能.我觉着挺好,方便刚開始学习的人分类练习.同一时候也是解题时的思路提示. [题目] Suppose a sort ...

  9. 【leetcode】Find Minimum in Rotated Sorted Array I&&II

    题目概述: Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 ...

随机推荐

  1. BZOJ3129方程(SDOI2013)

    https://blog.csdn.net/Maxwei_wzj/article/details/80152116 对变量有上界限制及下界限制.对于下界,可以从总数中减去即可,对于上界,容斥定理.

  2. 【VS开发】【图像处理】双边滤波器bilateral filter

    目录(?)[-] 简介 原理 代码实现 1 Spatial Weight 2 Similarity Weight 3 Color Filtering 在SSAO中的使用 1. 简介 图像平滑是一个重要 ...

  3. 禁止layer.msg()回调函数时抖动

    layer.msg(resp.msg, { shift: -1, time: 2000 }, function () {                                window.l ...

  4. Akka系列(五):Java和Scala中的Future

    前言....... 随着CPU的核数的增加,异步编程模型在并发领域中的得到了越来越多的应用,由于Scala是一门函数式语言,天然的支持异步编程模型,今天主要来看一下Java和Scala中的Futrue ...

  5. 交换机安全学习笔记 第四章 VLAN

      Trunk 口  思科称为:native VLAN  华为称为:PVID   说白了就是Trunk端口本身所属的VLAN,因为,Trunk端口要"透传"多个VLAN的流量,其本 ...

  6. A9-USART2_RX_BUF 串口2收发异常

    a9_send_cmd(); //退出透传模式,和前一次发送时间超过 2 秒,输入+++,就可以退出透传模式 delay_ms(); delay_ms(); delay_ms(); a9_quit_t ...

  7. 项目中使用vue的API。 和项目的结构

    <template> <!--组件的 结构--> <div id="app"> <h3>{{ msg }}</h3> & ...

  8. Nmap Windows 版本时区显示乱码

    Nmap 版本 $ nmap --version Nmap version 7.80 ( https://nmap.org ) Platform: i686-pc-windows-windows Co ...

  9. 洛谷 P3857 彩灯 题解

    题面 对于每一个开关,我们可以看成一个0/1串,初始是一个全部为0的串,要求经过这些开关的操作后,出现的不同的0/1串的个数 建模就是存在一些数,这些数异或起来是0(等价于没有操作).那么需要求一个集 ...

  10. Codeforces 1220D. Alex and Julian

    传送门 首先考虑怎样的集合一定是合法的 发现全部是奇数的集合一定合法,因为每次都是奇数连偶数,偶数连奇数 然后考虑如果集合同时有奇数和偶数是否一定不合法,结论是一定不合法,证明如下: 设某个奇数为 $ ...