leetcode 874. Walking Robot Simulation(python)

263 阅读2分钟

这是我参与更文挑战的第5天,活动详情查看: 更文挑战

描述

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 12 + 82 = 65 units away.

Note:

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

解析

根据题意,就是找出机器人走完路线之后,得到的最大的欧式距离。思路很简单,就是根据命令找准东南西北四个方向中的一个,然后走若干步数,计算当前最大的欧氏距离。这里的难点在于计算完点位之后如果是障碍点就要退回之前的那个点位并且不再继续走,而是等待下一个命令。这里的 obstacles 要去重,否则会爆 Time Limit Exceeded ,哭晕。

解答

class Solution(object):
    def robotSim(self, commands, obstacles):
        """
        :type commands: List[int]
        :type obstacles: List[List[int]]
        :rtype: int
        """
        position = [0, 0]
        directions = ['N', 'E', 'S', 'W']
        direction = 'N'
        max_distance = 0
        obstacles = set([(obstacle[0],obstacle[1]) for obstacle in obstacles])
        for operation in commands:
            if operation == -2:  # turn left 90 degrees,
                direction = directions[(directions.index(direction)-1)%4]
            elif operation == -1:  # -1: turn right 90 degrees
                direction = directions[(directions.index(direction)+1)%4]
            elif operation:
                for i in range(operation):
                    if direction == 'W':
                        position[0] -= 1
                        if (position[0],position[1]) in obstacles:
                            position[0] += 1
                            break
                    elif direction == 'N':
                        position[1] += 1
                        if (position[0],position[1]) in obstacles:
                            position[1] -= 1
                            break
                    elif direction == 'E':
                        position[0] += 1
                        if (position[0],position[1]) in obstacles:
                            position[0] -= 1
                            break
                    elif direction == 'S':
                        position[1] -= 1
                        if (position[0],position[1]) in obstacles:
                            position[1] += 1
                            break
                max_distance = max(position[0] ** 2 + position[1] ** 2, max_distance)
        return max_distance
        	      
		

运行结果

Runtime: 308 ms, faster than 61.36% of Python online submissions for Walking Robot Simulation.
Memory Usage: 19.9 MB, less than 31.82% of Python online submissions for Walking Robot Simulation.

原题链接:leetcode.com/problems/wa…

您的支持是我最大的动力