[leetcode] 874. Walking Robot Simulation

Description

A robot on an infinite XY-plane starts at point (0, 0) and faces north. The robot can receive one of three possible types of commands:

  • -2: turn left 90 degrees,
  • -1: turn right 90 degrees, or
  • 1 <= k <= 9: move forward k units.

Some of the grid squares are obstacles. The ith obstacle is at grid point obstacles[i] = (xi, yi).

If the robot would try to move onto them, the robot stays on the previous grid square instead (but still continues following the rest of the route.)

Return the maximum Euclidean distance that the robot will be from the origin squared (i.e. if the distance is 5, return 25).

Note:

  • North means +Y direction.
  • East means +X direction.
  • South means -Y direction.
  • West means -X direction.

Example 1:

Input: commands = [4,-1,3], obstacles = []
Output: 25
Explanation: The robot starts at (0, 0):
1. Move north 4 units to (0, 4).
2. Turn right.
3. Move east 3 units to (3, 4).
The furthest point away from the origin is (3, 4), which is 32 + 42 = 25 units away.

Example 2:

Input: commands = [4,-1,4,-2,4], obstacles = [[2,4]]
Output: 65
Explanation: The robot starts at (0, 0):
1. Move north 4 units to (0, 4).
2. Turn right.
3. Move east 1 unit and get blocked by the obstacle at (2, 4), robot is at (1, 4).
4. Turn left.
5. Move north 4 units to (1, 8).
The furthest point away from the origin is (1, 8), which is 1^2 + 8^2 = 65 units away.

Constraints:

  • 1 <= commands.length <= 104
  • commands[i] is one of the values in the list [-2,-1,1,2,3,4,5,6,7,8,9].
  • 0 <= obstacles.length <= 104
  • -3 * 104 <= xi, yi <= 3 * 104
  • The answer is guaranteed to be less than 2^31.

analysis

The meaning of the question is: a robot standing at the position (0,0), now given some commands, -2 means turning left 90 degrees, -1 means turning right 90 degrees, and a positive number means moving forward in the current direction. Steps, and then gave an array of obstacles obstacles, which is the place where the robot cannot pass. For example, there is an obstacle in front of the robot. At this time, the robot receives an instruction to go forward x steps, but due to obstacles, the instruction cannot be executed. The robot stayed in place.

The idea is very straightforward. After simulating the commands of the robot, you will know the final destination. Pay attention to the processing of the turning code when receiving the commands -2 and -1. -2 is to the left, then

di=(di-1)%4

-1 is to the right

di=(di+1)%4

You can simulate it against dirs=[[0,1],[1,0],[0,-1],[-1,0]] to see if this is the case. [0,1] becomes [1,0] to the right, and [1,0] to [0,1] is to the left, which is consistent with the above formula.

  • If the cmd is not a command, but the number of steps the robot needs to take, it is necessary to judge whether the arrived place is in obstacles, if it is, it cannot be reached, otherwise it can be reached, and the coordinates and res are updated. Note that the number of cmd steps is judged step by step by looping, and you cannot directly add cmd steps, and the obstacles coordinates may be skipped.

If you are interested, you can debug it yourself, see Code 2 for the debugging code

Code

class Solution:
    def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int:
        dirs=[[0,1],[1,0],[0,-1],[-1,0]]
        x=0
        y=0
        di=0
        obstacleSet=set(map(tuple,obstacles))
        res=0
        
        for cmd in commands:
            if(cmd==-2):
                di=(di-1)%4
            elif(cmd==-1):
                di=(di+1)%4
            else:
                for k in range(cmd):
                    x1=x+dirs[di][0]
                    y1=y+dirs[di][1]
                    if((x1,y1) not in obstacleSet):
                        x=x1
                        y=y1
                        res=max(res,x*x+y*y)
        return res

Code two

class Solution:
    def robotSim(self, commands, obstacles) -> int:
        dirs=[[0,1],[1,0],[0,-1],[-1,0]]
        x=0
        y=0
        di=0
        obstacleSet=set(map(tuple,obstacles))
        res=0
        print(obstacleSet)
        for cmd in commands:
            if(cmd==-2):
                di=(di-1)%4
            elif(cmd==-1):
                di=(di+1)%4
            else:
                for k in range(cmd):
                    x1=x+dirs[di][0]
                    y1=y+dirs[di][1]
                    if((x1,y1) not in obstacleSet):
                        x=x1
                        y=y1
                        res=max(res,x*x+y*y)
        return res

if __name__=="__main__":
    commands = [4,-1,4,-2,4]
    obstacles = [[2,4]]
    solution=Solution()
    res=solution.robotSim(commands,obstacles)
    print(res)

references

Solution
[LeetCode] 874. Walking Robot Simulation Walking Robot Simulation

Guess you like

Origin blog.csdn.net/w5688414/article/details/114488104