作者: 负雪明烛
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. python12类

    self 表示类里面的对象的引用 python一般不需要去解决内存管理,解释器会进行自动给回收 #类鱼类之间空格两行,前面的函数里面也是两行,类里面的方法个一行 class Cat(object): ...

  2. Linux-centos7设置静态IP地址

    参考:https://blog.csdn.net/sjhuangx/article/details/79618865

  3. 巩固javaweb的第二十七天

    巩固内容 正则表达式: 5. 指定字符串的开始和结尾 正则表达式中字符串的开始和结束符如表 2.6 所示. 表 2.6 开 始 和 结 尾 字符 作 用 ^ 指定以某个字符串开始 $ 指定以某个字符串 ...

  4. typedef定义数组

    typedef定义数组 问题来源 在学习高一凡数据结构与算法解析串这一章节时,遇到如下代码不明白其意义,经过查阅终于搞明白 typedef unsigned char SString[MAXLEN + ...

  5. A Child's History of England.15

    And indeed it did. For, the great army landing from the great fleet, near Exeter, went forward, layi ...

  6. 2021广东工业大学十月月赛 F-hnjhd爱序列

    题目:GDUTOJ | hnjhd爱序列 (gdutcode.cn) 一开始是用双指针从尾至头遍历,但发现会tle!! 后来朋友@77给出了一种用桶的做法,相当于是用空间换时间了. 其中用到的一个原理 ...

  7. Spring MVC入门(二)—— URI Builder模式

    URI Builder Spring MVC作为一个web层框架,避免不了处理URI.URL等和HTTP协议相关的元素,因此它提供了非常好用.功能强大的URI Builder模式来完成,这就是本文重点 ...

  8. spring cloud config center Git SSH configuration

    Git SSH configuration using properties By default, the JGit library used by Spring Cloud Config Serv ...

  9. jstl中的foreach标签

    <%@ page import="java.util.ArrayList" %><%@ page import="java.util.List" ...

  10. 『学了就忘』Linux服务管理 — 79、源码包安装的服务管理

    目录 1.源码包服务的启动管理 2.源码包服务的自启动管理 3.让源码包服务被服务管理命令识别 1.源码包服务的启动管理 # 通过源码包的安装路径,找到该服务的启动脚本, # 也就是获得该服务的启动脚 ...