Walking Robot Simulation II Problem
Description
LeetCode Problem 2069.
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 <= 10^5- At most
10^4calls in total will be made tostep,getPos, andgetDir.
Solution
Approach: Case analysis on the perimeter
Let mx = width - 1 and my = height - 1. The robot only ever walks along the border of the grid, a loop of length p = 2 * mx + 2 * my. Track a single position cur on that loop, starting at the bottom-left corner. It is split into four stretches:
0..mx: along the bottom edge, from(0, 0)to(mx, 0), facing East.mx+1..mx+my: up the right edge, facing North.mx+my+1..2*mx+my: along the top edge towards the left, facing West.2*mx+my+1..p-1: down the left edge, facing South.
step(num) is then just cur = (cur + num) % p, which also makes huge step counts cheap. getPos and getDir read the coordinates and direction off the stretch that cur falls in.
Two corner cases: a robot that has never moved faces East, and a robot that has moved and sits at (0, 0) (cur == 0) arrived from the left edge, so it faces South. Corners (mx, 0), (mx, my) and (0, my) belong to the stretch that ends there, which gives the direction the problem expects.
Complexity: O(1) time per operation; O(1) space.
class Robot:
def __init__(self, width: int, height: int):
self.mx = width - 1
self.my = height - 1
self.p = 2 * self.mx + 2 * self.my
self.cur = 0
self.moved = False
def step(self, num: int) -> None:
self.moved = True
self.cur = (self.cur + num) % self.p
def getPos(self) -> List[int]:
d = self.cur
mx, my = self.mx, self.my
if 0 <= d <= mx:
return [d, 0]
if mx < d <= mx + my:
return [mx, d - mx]
if mx + my < d <= 2 * mx + my:
return [mx - (d - (mx + my)), my]
return [0, my - (d - (2 * mx + my))]
def getDir(self) -> str:
d = self.cur
mx, my = self.mx, self.my
if not self.moved:
return "East"
if 1 <= d <= mx:
return "East"
elif mx < d <= mx + my:
return "North"
elif mx + my < d <= 2 * mx + my:
return "West"
return "South"
# Your Robot object will be instantiated and called as such:
# obj = Robot(width, height)
# obj.step(num)
# param_2 = obj.getPos()
# param_3 = obj.getDir()