Description
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.
- Attempts to move forward one cell in the direction it is facing.
- 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 thewidth x heightgrid with the robot at(0, 0)facing"East".void step(int num)Instructs the robot to move forwardnumsteps.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:
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 <= 1001 <= num <= 105- At most
104calls in total will be made tostep,getPos, andgetDir.
Solutions
This code simulates a robot walking around the perimeter of a rectangular grid. The robot starts at position [0, 0] facing East (direction index 1) and moves by taking steps in one of four directions: North, East, South, or West. When you call the step(num) method with a number, the robot attempts to move num steps in its current direction; if it would go out of bounds, it turns left (counter-clockwise by adding 3 to the direction and using modulo 4) and continues trying to step until all num steps are consumed. The key optimization is that each iteration calculates the farthest the robot can go in one direction before hitting a boundary, then subtracts that distance from the remaining steps—this avoids simulating each individual step. The robot's current position and direction can be retrieved with getPos() and getDir(), and the perimeter is pre-calculated to handle wrapping around the rectangle's edge (width * 2 + height * 2 - 4 accounts for the corners not being double-counted).
const directions = ['North', 'East', 'South', 'West'];
const moves = [[0, 1], [1, 0], [0, -1], [-1, 0]];
/**
* @param {number} width
* @param {number} height
*/
var Robot = function(width, height) {
this.width = width;
this.height = height;
this.pos = [0, 0];
this.direction = 1;
this.perimeter = width * 2 + height * 2 - 4;
};
/**
* @param {number} num
* @return {void}
*/
Robot.prototype.step = function(num) {
while (num > 0) {
const x = this.pos[0];
const y = this.pos[1];
const nx = Math.max(0, Math.min(x + moves[this.direction][0] * num, this.width - 1));
const ny = Math.max(0, Math.min(y + moves[this.direction][1] * num, this.height - 1));
if (nx === x && ny === y) {
this.direction = (this.direction + 3) % 4;
continue;
}
this.pos = [nx, ny];
num = (num - Math.abs(x - nx) - Math.abs(y - ny)) % this.perimeter;
}
};
/**
* @return {number[]}
*/
Robot.prototype.getPos = function() {
return this.pos;
};
/**
* @return {string}
*/
Robot.prototype.getDir = function() {
return directions[this.direction];
};
/**
* Your Robot object will be instantiated and called as such:
* var obj = new Robot(width, height)
* obj.step(num)
* var param_2 = obj.getPos()
* var param_3 = obj.getDir()
*/