鱼C论坛

 找回密码
 立即注册
查看: 2421|回复: 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 is in {'G', 'L', 'R'}


  1. class Solution:
  2.     def isRobotBounded(self, instructions: str) -> bool:
  3.         # N,W,S,E
  4.         directions = [[0, 1],[-1, 0],[0,-1],[1,0]]
  5.         direction = 0
  6.         x, y = 0, 0
  7.         
  8.         for instruction in instructions:
  9.             if instruction == 'G':
  10.                 x = x + directions[direction][0]
  11.                 y = y + directions[direction][1]
  12.             elif instruction == 'L':
  13.                 direction = (direction + 1) % 4
  14.             else:
  15.                 direction = (direction + 3) % 4
  16.         return (x == 0 and y == 0) | direction != 0
复制代码

本帖被以下淘专辑推荐:

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-24 21:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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