【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: 9Constraints:
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的更多相关文章
- 【leetcode】453. Minimum Moves to Equal Array Elements
problem 453. Minimum Moves to Equal Array Elements 相当于把不等于最小值的数字都减到最小值所需要次数的累加和. solution1: class So ...
- 【LeetCode】453. Minimum Moves to Equal Array Elements 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:模拟过程 方法二:求和-n*最小值 方法三: ...
- 【LeetCode】462. Minimum Moves to Equal Array Elements II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:排序 方法二:直接找中位数 日期 题目地址: ...
- 【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 ...
- 【leetcode】963. Minimum Area Rectangle II
题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...
- 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)
[LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...
- 【leetcode】712. Minimum ASCII Delete Sum for Two Strings
题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...
- 【LeetCode】Find Minimum in Rotated Sorted Array 解题报告
今天看到LeetCode OJ题目下方多了"Show Tags"功能.我觉着挺好,方便刚開始学习的人分类练习.同一时候也是解题时的思路提示. [题目] Suppose a sort ...
- 【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 ...
随机推荐
- [转帖]Windows与Linux的命令行命令对比
Windows与Linux的命令行命令对比 https://www.cnblogs.com/sztom/p/10785140.html * Windows不区分大小写,Linux区分大小写的. sn ...
- [转帖]浅谈IOC--说清楚IOC是什么
浅谈IOC--说清楚IOC是什么 Need Study https://www.cnblogs.com/DebugLZQ/archive/2013/06/05/3107957.html 博文目录 1. ...
- javaSE温习一&二
这是一个简单的笔记 涉及到常量.变量:流程控制语句.数组:类与对象.封装.构造方法:Scanner类.Random类.Arraylist类: 1.pubic class static void 2. ...
- 洛谷P1600 天天爱跑步——题解
题目传送 首先要考虑入手点.先考虑一个一个玩家处理,显然不加优化的话,时间复杂度是O(n)的.发现对于玩家路径上的点都有一个观察员,一个都不能忽视,看起来是很难优化了.在做题时,发现一个思路很难想,就 ...
- 文件的三种打开方式及with管理文件上下文
文件的三种打开方式及with管理文件上下文 一.文件的三种打开方式 1.1 只读 f = open(r'D:\pycharm\yjy\上海python学习\456.txt','r',encoding= ...
- ActionsChains类鼠标事件和Keys类键盘事件
一.鼠标事件 如,移动.点击.释放.单击.右击,拖动等 键盘事件如:输入.回车.粘贴.复制.剪贴等 使用ActionsChains类和Keys类之前都必须先导入 from selenium ...
- ds replicas是什么
以上是我百度的答案.很显然,这个对于初学者很不友好,中文的每一个靠谱的,英文的都TM是how 和 why .But 我要 what! 所以只能自己搞,自己琢磨,搞完分享出来,助力后来者不掉坑,有所查. ...
- allure 这么高大上的测试报告环境,5 分钟搞定
allure 的测试报告是老板喜欢的样子.如果能用上 allure,干嘛还选择其他的测试报告类型呢?python 的 pytest 单元测试框架有 allure 的插件,可以很方便的在 python ...
- js实现简繁转换
document.write(" <a name=\"StranLink\" id=\"StranLink\" style=\"col ...
- sql server 对数运算函数log(x)和log10(x)
--LOG(x)返回x的自然对数,x相对于基数e的对数 --LOG10(x)返回x的基数为10的对数 示例:select LOG(3),LOG(6),LOG10(1),LOG10(100),LOG10 ...