题目如下:

力扣团队买了一个可编程机器人,机器人初始位置在原点(0, 0)。小伙伴事先给机器人输入一串指令command,机器人就会无限循环这条指令的步骤进行移动。指令有两种:

U: 向y轴正方向移动一格
R: 向x轴正方向移动一格。
不幸的是,在 xy 平面上还有一些障碍物,他们的坐标用obstacles表示。机器人一旦碰到障碍物就会被损毁。

给定终点坐标(x, y),返回机器人能否完好地到达终点。如果能,返回true;否则返回false。

示例 1:

输入:command = "URR", obstacles = [], x = 3, y = 2
输出:true
解释:U(0, 1) -> R(1, 1) -> R(2, 1) -> U(2, 2) -> R(3, 2)。
示例 2:

输入:command = "URR", obstacles = [[2, 2]], x = 3, y = 2
输出:false
解释:机器人在到达终点前会碰到(2, 2)的障碍物。
示例 3:

输入:command = "URR", obstacles = [[4, 2]], x = 3, y = 2
输出:true
解释:到达终点后,再碰到障碍物也不影响返回结果。

限制:

2 <= command的长度 <= 1000
command由U,R构成,且至少有一个U,至少有一个R
0 <= x <= 1e9, 0 <= y <= 1e9
0 <= obstacles的长度 <= 1000
obstacles[i]不为原点或者终点

解题思路:机器人的轨迹是有规律的。首先可以分别求出command中U和R的数量,然后遍历一次command,记录机器人运行时候经过的坐标。假如其中有一个坐标为(x1,y1),那么这个点接下来的轨迹满足 x1 = x1*r_count*n, y 1 = y1*u_count*n,n为正整数。所以只需要判断obstacles是否满足上面的方程即可。最后要注意的一点是也要判断终点是否满足这个方程,如果不满足说明从起点开始,即使没遇到任何障碍也无法到达终点。

代码如下:

class Solution(object):
def robot(self, command, obstacles, x, y):
"""
:type command: str
:type obstacles: List[List[int]]
:type x: int
:type y: int
:rtype: bool
"""
u_count = command.count('U')
r_count = command.count('R')
path = [[0,0]]
cx,cy = 0,0
for c in command:
if c == 'U':cy += 1
else: cx += 1
path.append([cx,cy])
for (ox,oy) in obstacles:
if ox > x or oy > y:continue
for (px,py) in path:
if (ox - px) % r_count == 0 and (oy - py) % u_count == 0 and (ox - px) / r_count == (oy - py) / u_count :
return False #check can reach exit
for (px, py) in path:
if (x - px) % r_count == 0 and (y - py) % u_count == 0 and (x - px) / r_count == (y - py) / u_count:
return True return False

【leetcode】LCP 3. Programmable Robot的更多相关文章

  1. 【leetcode】LCP 1. Guess Numbers

    题目如下: 小A 和 小B 在玩猜数字.小B 每次从 1, 2, 3 中随机选择一个,小A 每次也从 1, 2, 3 中选择一个猜.他们一共进行三次这个游戏,请返回 小A 猜对了几次? 输入的gues ...

  2. 【LeetCode】LCP 07. 传递信息

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...

  3. 【LeetCode】LCP 06. 拿硬币

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 替换 日期 题目地址:https://leetcode ...

  4. 【leetcode】LCP 2. 分式化简

    题目如下: 有一个同学在学习分式.他需要将一个连分数化成最简分数,你能帮助他吗? 连分数是形如上图的分式.在本题中,所有系数都是大于等于0的整数. 输入的cont代表连分数的系数(cont[0]代表上 ...

  5. 【leetcode】657. Robot Return to Origin

    Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...

  6. 【LeetCode】657. Judge Route Circle 解题报告

    [LeetCode]657. Judge Route Circle 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/judge-route- ...

  7. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  8. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  9. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

随机推荐

  1. beego项目部署到nginx(含http转https)

    beego项目部署到nginx(含http转https)    之前的程序部署到服务器采用的直接部署,比较方便,现在把它部署到nginx,以便后续的反向代理和负载均衡,同时,因为要接入微信小程序,所以 ...

  2. ArrayList(顺序表)和LinkedList(链表)的区别联系,优劣取舍问题

    ArrayList和LinkedList都是List接口的实现类.主要区别如下: 最主要的区别是底层的数据结构不同: 1)ArrayList相当于一个动态数组,需要随机访问列表中的元素时,ArrayL ...

  3. Spring(十)--Advisor顾问

    Spring之Advisor顾问 1. 创建新的xml文件  advisor.xml <!--01. 配置目标对象 实际肯定是配置UserServiceImpl--> <bean i ...

  4. MySql MediumBlob——MySql的Bolb四种类型

    MySQL中,BLOB是一个二进制大型对象,是一个可以存储大量数据的容器,它能容纳不同大小的数据.BLOB类型实际是个类型系列(TinyBlob.Blob.MediumBlob.LongBlob),除 ...

  5. Tomcat配置:java.lang.UnsatisfiedLinkError: D:\DevelopTool\tool20150402\tomcat\apache-tomcat-8.5.16\bin\tcnative-1.dll: Can't load AMD 64-bit .dll on a IA 32-bit platform

    解决办法: tomcat启动时提示java.lang.UnsatisfiedLinkError: D:\soft\devTool\apache-tomcat-7.0.57\bin\tcnative-1 ...

  6. 使用render函数渲染组件

    使用render函数渲染组件:https://www.jianshu.com/p/27ec4467a66b

  7. 2019icpc徐州网络赛

    A Who is better? 题意 excrt+斐波那契博弈 分析 Java的BigInteger对象默认为null,不能直接比较. 代码 import java.math.BigInteger; ...

  8. 11、权重残差图、RLE和NUSE

    affyPLM包可以对芯片原始数据进行拟合回归,最后得到芯片权重(Weights)残差(Residuals)图.相对对数表达(RLE,Relative log expression)箱线图.相对标准差 ...

  9. MySQL数据表操作命令

    mysql语句: 1.修改表名: rename table 旧表名 to 新表名; 2.修改字段类型: alter table 表名 modify column 字段名 字段类型(长度) 3.修改字段 ...

  10. _proto_和prototype的区别

    1. _proto_和prototype prototype属性是一个静态属性, _proto_属性是一个实例属性. prototype表示类的原型对象,_proto_表示原型对象中定义的内部属性[p ...