题目如下:

力扣团队买了一个可编程机器人,机器人初始位置在原点(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. nova计算服务

    1. nova介绍 Nova 是 OpenStack 最核心的服务,负责维护和管理云环境的计算资源.OpenStack 作为 IaaS 的云操作系统,虚拟机生命周期管理也就是通过 Nova 来实现的. ...

  2. 实现 laravel 的artisan

    laravel 的 artisan 命令行太好用了,换个框架没有这个功能,于是自己学习实现一些,直接上代码 新建目录 -artisan --bin --src 进入artisan composer i ...

  3. cosbench 安装

    cosbench是什么 COSBench是Intel团队基于java开发,衡量云对象存储服务性能的基准测试工具,全称是Cloud object Storage Bench,同所有的性能测试工具一样,C ...

  4. 写一个比较全的进制转换函数--ic

    //写一个比较全的进制转换函数-----未完成 #include <stdio.h> //D进制转换后 (比如10-2进制) 结果可能会很大 需要很长的字符串来存 #include < ...

  5. IDEA 对spring boot Maven 项目打 Jar 包

    <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> ...

  6. python 并发编程 协程 目录

    python 并发编程 协程 协程介绍 python 并发编程 协程 greenlet模块 python 并发编程 协程 gevent模块 python 并发编程 基于gevent模块实现并发的套接字 ...

  7. Vue.js官方文档学习笔记(二)组件化应用的构建

    组件化应用的构建 组件化应用允许我们使用小型.独立和通常可复用的组件构建大型应用. Vue注册组件 Vue.component('todo-item',{template:'<li>这是个 ...

  8. CF 631B 题解

    题面 注意到每次只染色一行或者一列,那么我们最后输出第i行第j列的数字是多少的时候只需要看一下最后一次i行和第j行被染了什么颜色,所以我们需要对每一行和一列记录最后一次染色的颜色. 但是我们也需要比较 ...

  9. [Codeforces 1005F]Berland and the Shortest Paths(最短路树+dfs)

    [Codeforces 1005F]Berland and the Shortest Paths(最短路树+dfs) 题面 题意:给你一个无向图,1为起点,求生成树让起点到其他个点的距离最小,距离最小 ...

  10. [LeetCode] 完全二叉树的节点个数

    题目链接: https://leetcode-cn.com/problems/count-complete-tree-nodes 难度:中等 通过率:57.4% 题目描述: 给出一个 完全二叉树 ,求 ...