2069. Walking Robot Simulation II (Medium)

发布时间 2023-07-19 10:19:33作者: zwyyy456

Description

2069. Walking Robot Simulation II (Medium)

A width x height grid is on an XY-plane with the bottom-left cell at (0, 0) and the top-right cell at (width - 1, height - 1). The grid is aligned with the four cardinal directions ("North", "East", "South", and "West"). A robot is initially at cell (0, 0) facing direction "East".

The robot can be instructed to move for a specific number of steps. For each step, it does the following.

  1. Attempts to move forward one cell in the direction it is facing.
  2. If the cell the robot is moving to is out of bounds, the robot instead turns 90 degrees counterclockwise and retries the step.

After the robot finishes moving the number of steps required, it stops and awaits the next instruction.

Implement the Robot class:

  • Robot(int width, int height) Initializes the width x height grid with the robot at (0, 0) facing "East".
  • void step(int num) Instructs the robot to move forward num steps.
  • int[] getPos() Returns the current cell the robot is at, as an array of length 2, [x, y].
  • String getDir() Returns the current direction of the robot, "North", "East", "South", or "West".

 

Example 1:

example-1
Input
["Robot", "step", "step", "getPos", "getDir",
"step", "step", "step", "getPos", "getDir"]
[[6, 3], [2], [2], [], [], [2], [1], [4], [], []]
Output
[null, null, null, [4, 0], "East", null, null, null, [1, 2], "West"]

Explanation Robot robot = new Robot(6, 3); // Initialize the grid and the robot at (0, 0) facing East. robot.step(2); // It moves two steps East to (2, 0), and faces East. robot.step(2); // It moves two steps East to (4, 0), and faces East. robot.getPos(); // return [4, 0] robot.getDir(); // return "East" robot.step(2); // It moves one step East to (5, 0), and faces East. // Moving the next step East would be out of bounds, so it turns and faces North. // Then, it moves one step North to (5, 1), and faces North. robot.step(1); // It moves one step North to (5, 2), and faces North (not West). robot.step(4); // Moving the next step North would be out of bounds, so it turns and faces West. // Then, it moves four steps West to (1, 2), and faces West. robot.getPos(); // return [1, 2] robot.getDir(); // return "West"

 

Constraints:

  • 2 <= width, height <= 100
  • 1 <= num <= 105
  • At most 104 calls in total will be made to step, getPos, and getDir.

Solution

This is a somewhat tricky simulation problem, but it becomes straightforward when you notice two points:

  1. The robot will only be positioned on the outermost layer of the grid.
  2. We can keep track of the total number of steps the robot has moved. This way, we can calculate the paths starting from $(0, 0)$ without needing to use the robot's previous position.

Code

class Robot {
  public:
    Robot(int width, int height) {
        wid_ = width;
        hei_ = height;
    }

    void step(int num) {
        total += num;
        if (total == 0) {
            return;
        }
        int realmove = total % (wid_ - 1 + hei_ - 1 + wid_ - 1 + hei_ - 1);
        if (realmove == 0) {
            direct = 3;
            posx = 0;
            posy = 0;
        } else if (realmove > 0 && realmove <= wid_ - 1) {
            direct = 0;
            posx = realmove;
            posy = 0;
        } else if (realmove > wid_ - 1 && realmove <= wid_ - 1 + hei_ - 1) {
            posx = wid_ - 1;
            posy = realmove - (wid_ - 1);
            direct = 1;
        } else if (realmove > wid_ - 1 + hei_ - 1 && realmove <= wid_ - 1 + hei_ - 1 + wid_ - 1) {
            posy = hei_ - 1;
            posx = wid_ - 1 - (realmove - (wid_ - 1 + hei_ - 1));
            direct = 2;
        } else {
            posx = 0;
            posy = hei_ - 1 - (realmove - (wid_ - 1 + hei_ - 1 + wid_ - 1));
            direct = 3;
        }
    }

    vector<int> getPos() {
        return {posx, posy};
    }

    string getDir() {
        switch (direct % 4) {
        case 0:
            return "East";
        case 1:
            return "North";
        case 2:
            return "West";
        case 3:
            return "South";
        }
        return "";
    }

  private:
    int posx = 0;
    int posy = 0;
    int direct = 0;
    int wid_;
    int hei_;
    int total = 0;
};