作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/robot-bounded-in-circle/

题目描述

On an infinite plane, a robot initially stands at (0, 0) and faces north. The robot can receive one of three instructions:

  • “G”: go straight 1 unit;
  • “L”: turn 90 degrees to the left;
  • “R”: turn 90 degress to the right.

The robot performs the instructions given in order, and repeats them forever.

Return true if and only if there exists a circle in the plane such that the robot never leaves the circle.

Example 1:

Input: "GGLLGG"
Output: true
Explanation:
The robot moves from (0,0) to (0,2), turns 180 degrees, and then returns to (0,0).
When repeating these instructions, the robot remains in the circle of radius 2 centered at the origin.

Example 2:

Input: "GG"
Output: false
Explanation:
The robot moves north indefinitely.

Example 3:

Input: "GL"
Output: true
Explanation:
The robot moves from (0, 0) -> (0, 1) -> (-1, 1) -> (-1, 0) -> (0, 0) -> ...

Note:

  1. 1 <= instructions.length <= 100
  2. instructions[i] is in {'G', 'L', 'R'}

题目大意

机器人初始位置在原点,面向北。接受到一串指令,G代表直走1步;L代表左转;R代表右转。指令会无限的循环下去。问是否会有一个圆,把机器人的路径包括?

解题方法

找规律

我们一定知道,这个题不能用暴力解法。机器人的路径最终被圆包括的充分条件是机器在一些指令之后回到原点。由于指令是循环的,题目只给出了部分指令。那么对于该部分指令的要求是,机器人回到了原点或者不再原点且不面向北

回到原点很好理解,那么不再原点且不面向北是什么意思呢?

如果题目中的指令结束之后,机器人不在原点,那么说明它相对原点移动了一个向量v。机器人在指令结束后的位置成为了新的原点,题目说机器人的初始状态时是面向北的,那么如果在新的原点上仍然面向北,那么一定还会继续向第一次一样相对新的原点移动相同的向量v,此时机器人距原点的向量是2v。

那么为什么不面向北就一定能回到原点呢?这是由于在新的原点新的方向上再次接受相同指令后,机器人移动新向量的长度为|向量v|、方向和v成90度或者180度;多次指令的结果叠加就会抵消第一次移动的v。

Python代码如下:

class Solution(object):
def isRobotBounded(self, instructions):
"""
:type instructions: str
:rtype: bool
"""
dirs = [(0, 1), (-1, 0), (0, -1), (1, 0)]
x, y = 0, 0
curd = 0
for i in instructions:
if i == "G":
x += dirs[curd][0]
y += dirs[curd % 4][1]
elif i == "L":
curd = (curd + 1) % 4
elif i == "R":
curd = (curd - 1) % 4
return (x == 0 and y == 0) or curd != 0

从上面的说明中我们看出来,如果要把第一次的向量v进行抵消,那么指令最多重复4次即可。即该机器人的路径能不能形成圆的充分必要条件是该指令重复4次后机器人能回到原点。

class Solution(object):
def isRobotBounded(self, instructions):
"""
:type instructions: str
:rtype: bool
"""
dirs = [(0, 1), (-1, 0), (0, -1), (1, 0)]
x, y = 0, 0
curd = 0
for i in instructions * 4:
if i == "G":
x += dirs[curd][0]
y += dirs[curd % 4][1]
elif i == "L":
curd = (curd + 1) % 4
elif i == "R":
curd = (curd - 1) % 4
return x == 0 and y == 0

参考资料:
https://leetcode.com/problems/robot-bounded-in-circle/discuss/304977/1041.-C-Solution-Beats-100-Runtime-and-100-Memory-(with-explanation)

日期

2019 年 6 月 15 日 —— 今天这个题有意思

【LeetCode】1041. Robot Bounded In Circle 解题报告(Python)的更多相关文章

  1. LeetCode 1041. Robot Bounded In Circle (困于环中的机器人)

    题目标签:Math 题目让我们判断机器人是否是一直在走一个圈. 当我们把 instructions 走完一遍时候: 1. 如果机器人回到了原点,那么它是在走一个圈. 2. 如果机器人的方向没有改变,那 ...

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

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

  3. 【leetcode】1041. Robot Bounded In Circle

    题目如下: On an infinite plane, a robot initially stands at (0, 0) and faces north.  The robot can recei ...

  4. LeetCode 657 Robot Return to Origin 解题报告

    题目要求 There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of it ...

  5. 1041. Robot Bounded In Circle

    本题题意: 一开始一个机器人站在了(0,0)上,面朝的方向是北,收到三个序列G,L,R. G:直走 L:向左转 R:向右转 按序执行,永远重复. 返回TRUE,如果处在一个圈. 第一个卡住的点: 1. ...

  6. 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...

  7. 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)

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

  8. 【LeetCode】784. Letter Case Permutation 解题报告 (Python&C++)

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

  9. 【LeetCode】760. Find Anagram Mappings 解题报告

    [LeetCode]760. Find Anagram Mappings 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/find ...

随机推荐

  1. 如何根据taxid(或taxname)快速获得taxname(或taxid)?

    目录 需求 实现 需求 我有一个物种taxonomy ID的list,想获得相应的物种名,不要一个个去NCBI Taxonomy官网查.反之根据物种名list查询对应的taxid. 实现 因为之前没怎 ...

  2. 使用SpringBoot实现文件的上传

    使用SpringBoot实现文件的上传 springboot可以直接使用 org.springframework.web.multipart.MultipartFile 所以非常容易实现 一.首先是简 ...

  3. 07 MySQL安装图解--Windows版本

    MySQL安装图解 使用微信扫码关注微信公众号,并回复:"MySQL环境",免费获取下载链接! 1.安装MySQL 2.校验MySQL 3.登录MySQL 登录MySQL:mysq ...

  4. day25 组合和内置函数

    day25 组合和内置函数 一.组合 # 解决类与类之间代码冗余问题: 1. 继承 2. 组合 组合:一个对象拥有一个属性, 属性的值必须是另外一个对象 继承满足的是:什么是什么的关系 # is-a ...

  5. Scala(二)【基本使用】

    一.变量和数据类型 1.变量 语法:val / var 变量名:变量类型 = 值 val name:String = "zhangsan" 注意 1.val定义的变量想到于java ...

  6. 大数据学习day25------spark08-----1. 读取数据库的形式创建DataFrame 2. Parquet格式的数据源 3. Orc格式的数据源 4.spark_sql整合hive 5.在IDEA中编写spark程序(用来操作hive) 6. SQL风格和DSL风格以及RDD的形式计算连续登陆三天的用户

    1. 读取数据库的形式创建DataFrame DataFrameFromJDBC object DataFrameFromJDBC { def main(args: Array[String]): U ...

  7. 安全相关,xss

    XSS XSS,即 Cross Site Script,中译是跨站脚本攻击:其原本缩写是 CSS,但为了和层叠样式表(Cascading Style Sheet)有所区分,因而在安全领域叫做 XSS. ...

  8. 【Reverse】每日必逆0x01

    附件:https://files.buuoj.cn/files/7458c5c0ce999ac491df13cf7a7ed9f1/SimpleRev 题解 查壳 64位ELF文件,无壳 IDApro处 ...

  9. java面试--(生成随机数,获取重复次数最多,并且数是最大的一个,打印出来)

    import java.util.*; public class MaxRandom { public static void main(String[] args){ int[] num = new ...

  10. Js判断数组中是否存在某个元素

    Js判断数组中是否存在某个元素 方法一:indexOf(item,start); Item:要查找的值:start:可选的整数参数,缺省则从起始位子开始查找. indexOf();返回元素在数组中的位 ...