【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 ...
随机推荐
- Eclipse 包含头文件 添加环境变量
Eclipse 中新建C 或C ++到项目时,头文件报警,显示“Unresolved inclusion:<stdio.h>” 虽然不影响项目到编译和运行,确也无法查看头文件,让人感觉实在 ...
- poj2352(树状数组)
题目链接:https://vjudge.net/problem/POJ-2352 题意:在直角坐标系中给出n个点的 (x,y),(0<=x,y<=32000),定义每个点的level为(x ...
- sql server CDC报错:超出存储过程、函数、触发器的最大嵌套层数(最大层为32)
sys.sp_MScdc_capture_job RAISERROR(22801, 10, -1) --原本 go sys.sp_MScdc_capture_job; go --修改后 ...
- Atomic实现原子性源码分析:CAS(比较并交换)、Unsafe类
1.CAS: 比较并交换(Compare And Swap),是Unsafe类中的一条CPU系统原语,原语的执行必须是连续的,在执行过程中不允许被中断,即CAS是一条CPU的原子指令,不会造成数据不一 ...
- mongodb增删改查常用命令总结
前言 去年我还折腾过mongodb,后来用不到也就没碰了,这就导致了我忘的一干二净,不得不感叹,编程这东西只要不用,就会忘没了.现在我想重拾mongodb,来总结一下常用命令,主要就是增删改查. 另外 ...
- [BZOJ 4820] [SDOI2017] 硬币游戏(高斯消元+概率论+字符串hash)
[BZOJ 4820] [SDOI2017] 硬币游戏(高斯消元+概率论+字符串hash) 题面 扔很多次硬币后,用H表示正面朝上,用T表示反面朝上,会得到一个硬币序列.比如HTT表示第一次正面朝上, ...
- git部分命令笔记
目录 配置user信息 建Git仓库 清空暂存区 git变更文件名 查看暂存区状态 查看历史 查看本地分支 查看所有分支(包含远程) 创建分支 基于远程分支创建本地新分支 查看图形化分支日志 图形化界 ...
- 使用Koa.js离不开这十个中间件
随着ES6的普及,async/await的语法受到更多JS开发者的青睐,Koa.js作为比较早支持使用该语法的Node框架越来越受到大家的喜爱,虽然Koa.js本身支持的功能很有限,但官方和社区提供了 ...
- Linux五大网络IO模型图解
对于一个应用程序即一个操作系统进程来说,它既有内核空间(与其他进程共享),也有用户空间(进程私有),它们都是处于虚拟地址空间中.用户进程是无法访问内核空间的,它只能访问用户空间,通过用户空间去内核空间 ...
- Python 进阶篇
作者:武沛齐 出处:http://www.cnblogs.com/wupeiqi/articles/5246483.html Model 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这 ...