1041. Robot Bounded In Circle

Robot Bounded In Circle - LeetCode

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 degrees 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: instructions = “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: instructions = “GG” Output: false Explanation: The robot moves north indefinitely. Example 3: Input: instructions = “GL” Output: true Explanation: The robot moves from (0, 0) -> (0, 1) -> (-1, 1) -> (-1, 0) -> (0, 0) -> …

Constraints:

1 <= instructions.length <= 100
instructions[i] is 'G', 'L' or, 'R'.

  • code naive
class Solution:
    def isRobotBounded(self, instructions: str) -> bool:
        pd = (0, 0, (0, 1))
        visited = set([pd])
        
        dr = {(0,1):(1,0), (1,0):(0,-1),(0,-1):(-1,0),(-1,0):(0,1)}
        dl = {(0,1):(-1,0), (-1,0):(0,-1),(0,-1):(1,0),(1,0):(0,1)}
        
        for round in range(4):
            for ins in instructions:
                *p, d = pd
                if ins == "G":
                    pd = (p[0] + d[0], p[1] + d[1], d)
                elif ins == "L":
                    pd = (*p, dl[d])
                elif ins == "R": # 0,1 -> 1,0  0,-1-> -1,0  -1,0->0,1 1,0 ->0,-1
                    pd = (*p, dr[d])
            if pd in visited: return True
        
        return False
        
  • code one round
class Solution:
    def isRobotBounded(self, instructions: str) -> bool:
        pd = (0, 0, (0, 1))
        dr = {(0,1):(1,0),(1,0):(0,-1),(0,-1):(-1,0),(-1,0):(0,1)}
        dl = {(0,1):(-1,0),(-1,0):(0,-1),(0,-1):(1,0),(1,0):(0,1)}
        
        for ins in instructions:
            *p, d = pd
            if ins == "G":
                pd = (p[0] + d[0], p[1] + d[1], d)
            elif ins == "L":
                pd = (*p, dl[d])
            elif ins == "R":
                pd = (*p, dr[d])
        return pd[0] == pd[1] == 0 or pd[2] != (0, 1)
        
  • code direction optimised
class Solution:
    def isRobotBounded(self, instructions: str) -> bool:
        pd = (0, 0, 0) # position, direction index
        ds = [(0,1), (1,0), (0,-1),(-1,0)]
        # dr = {ds[0]:ds[1],ds[1]:ds[2],ds[2]:ds[3],ds[3]:ds[0]}
        # dl = {(0,1):(-1,0),(-1,0):(0,-1),(0,-1):(1,0),(1,0):(0,1)}
        
        for ins in instructions:
            *p, d = pd
            if ins == "G":
                pd = (p[0] + ds[d][0], p[1] + ds[d][1] , d)
            elif ins == "L":
                pd = (*p, (d + 3) % 4)
            elif ins == "R":
                pd = (*p, (d + 1) % 4)
        return pd[0] == pd[1] == 0 or pd[2] != 0