鱼C论坛

 找回密码
 立即注册
查看: 2727|回复: 0

[学习笔记] Leetcode 1041. Robot Bounded In Circle

[复制链接]
发表于 2020-9-18 03:02:30 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
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 <= instructions.length <= 100
instructions[i] is in {'G', 'L', 'R'}

class Solution:
    def isRobotBounded(self, instructions: str) -> bool:
        # N,W,S,E
        directions = [[0, 1],[-1, 0],[0,-1],[1,0]]
        direction = 0
        x, y = 0, 0
        
        for instruction in instructions:
            if instruction == 'G':
                x = x + directions[direction][0]
                y = y + directions[direction][1]
            elif instruction == 'L':
                direction = (direction + 1) % 4
            else:
                direction = (direction + 3) % 4
        return (x == 0 and y == 0) | direction != 0

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-12-22 17:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表