We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Walking Robot Simulation II

Difficulty: Medium


Problem 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 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 required number of steps, 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".

Key Insights

  • The robot's movement is determined by its current direction, which can change if it encounters the grid boundaries.
  • The robot can only move one step at a time, requiring it to check for boundary conditions before moving.
  • Turning counterclockwise is a simple adjustment of the direction the robot is facing.

Space and Time Complexity

Time Complexity: O(num) for each step call, where num is the number of steps requested. Each step may involve multiple checks and potential turns. Space Complexity: O(1), since the robot's position and direction are stored with a fixed amount of variables.


Solution

To solve the problem, we can use an array to represent the directions (0: East, 1: North, 2: West, 3: South). The robot's current position is tracked using two variables (x, y). The robot moves by updating these variables based on its current direction. If the robot encounters a boundary, it updates its direction by incrementing a direction index, which effectively turns the robot counterclockwise. The solution is implemented using a class structure to encapsulate the robot's properties and methods.


Code Solutions

class Robot:
    def __init__(self, width: int, height: int):
        self.width = width
        self.height = height
        self.x = 0  # Initial x position
        self.y = 0  # Initial y position
        self.directions = ["East", "North", "West", "South"]  # Possible directions
        self.dir_index = 0  # Start facing East

    def step(self, num: int) -> None:
        for _ in range(num):
            if self.dir_index == 0:  # East
                if self.x + 1 < self.width:
                    self.x += 1
                else:
                    self.dir_index = (self.dir_index + 1) % 4  # Turn counterclockwise
            elif self.dir_index == 1:  # North
                if self.y + 1 < self.height:
                    self.y += 1
                else:
                    self.dir_index = (self.dir_index + 1) % 4
            elif self.dir_index == 2:  # West
                if self.x - 1 >= 0:
                    self.x -= 1
                else:
                    self.dir_index = (self.dir_index + 1) % 4
            elif self.dir_index == 3:  # South
                if self.y - 1 >= 0:
                    self.y -= 1
                else:
                    self.dir_index = (self.dir_index + 1) % 4

    def getPos(self) -> List[int]:
        return [self.x, self.y]

    def getDir(self) -> str:
        return self.directions[self.dir_index]
← Back to All Questions