算法:874. Walking Robot Simulation模拟机器人走路

Posted 架构师易筋

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了算法:874. Walking Robot Simulation模拟机器人走路相关的知识,希望对你有一定的参考价值。

874. Walking Robot Simulation

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.

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 231.

定义东南西北方向后的解法

机器人从点 (0, 0) 开始并面向北。网格的哪条边在北边?
由于它将使用 到达点 (3, 4) commands = [4,-1,3], obstacles = [],我们知道右边缘指向North。

     W
  S -|- N
     E

我们如何表示仅给定相对转向方向(即向左或向右)的绝对方向?我们定义direction表示绝对方向如下:

North, direction = 0, directions[direction] = {0, 1}
East,  direction = 1, directions[direction] = {1, 0}
South, direction = 2, directions[direction] = {0, -1}
West,  direction = 3, directions[direction] = {-1, 0}

direction will increase by one when we turn right,
and will decrease by one (or increase by three) when we turn left.

解法如下:

class Solution {
    public int robotSim(int[] commands, int[][] obstacles) {
        // north, east, south, west
        int[][] directionArray = {{0,1}, {1, 0}, {0, -1}, {-1, 0}};
        Set<String> obstacleSet = new HashSet<>();
        for (int[] obstacle: obstacles) {
            obstacleSet.add(obstacle[0] + "-" + obstacle[1]);
        }
        int direction = 0;
        int x = 0;
        int y = 0;
        int d = 0;
        
        for (int command: commands) {
            if (command == -2) {
                // turn left
                direction = (direction + 3) % 4;
            } else if (command == -1) {
                // turn right
                direction = (direction + 1) % 4;
            } else {
                int step = 0;
                while (step < command && !obstacleSet.contains((x + directionArray[direction][0]) + "-" + (y + directionArray[direction][1]))) {
                    x = x + directionArray[direction][0];
                    y = y + directionArray[direction][1];
                    step++;
                }
                d = Math.max(d, (x * x + y * y));
            }
        }
        
        return d;
    }
}

参考

https://leetcode.com/problems/walking-robot-simulation/discuss/155682/Logical-Thinking-with-Clear-Code

以上是关于算法:874. Walking Robot Simulation模拟机器人走路的主要内容,如果未能解决你的问题,请参考以下文章

874. Walking Robot Simulation

Leetcode_easy874. Walking Robot Simulation

*Leetcode 874. Walking Robot Simulation

874.Walking Robot Simulation(list不可被哈希)

leetcode 874 Robot Simulation

[LeetCode] Walking Robot Simulation 走路机器人仿真