Robot Bounded in Circle
2025-08-23
Problem Description
LeetCode Problem: Robot Bounded In Circle
Input/Output
Input - instructions: str
a string of instructions, which will be repeated forever.
1 <= instructions.length <= 100instructions[i]is'G','L'or'R'
For each instruction:
'G': moves the robot 1 step forward by adding the current direction to its position'L'and'R': rotate the direction vector 90 degrees
Output - True/False
- True: The robot is bounded in a circle (will return to origin eventually)
- False: The robot will move infinitely far from the origin
To determine if the robot is bounded, we check after one execution of the instructions:
True (Bounded):
final_pos == (0, 0)orfinal_dir != initial_dir- The robot can only face 4 directions, so if direction changes after one cycle, it will form a closed shape within 4 cycles
False (Unbounded):
final_pos != (0, 0)andfinal_dir == initial_dir
Analysis
To determine True/False, we need to track:
- The robot’s position
- Its direction
in 2D space
Direction options: To represent direction, we have two options:
- Update the angle θ and use (cosθ,sinθ)
- Simply choose from four unit vectors: (0,1), (1,0), (0,-1), (-1,0)
Here we choose the second option to solve it:
Solution
|
|
Complexity
- Time: O(n) - iterate through each instruction once
- Space: O(1) - only use fixed variables for position and direction
Generalization
This approach extends to higher dimensions with discrete rotations and variable step sizes:
Core Principle (Any Dimension):
- Bounded condition:
final_position == originORfinal_direction != initial_direction - Rotation constraints:
- 2D: Simple angles (90°, 60°, 45°) - rational multiples of 2π
- 3D: More complex - rotations around different axes, Euler angles
- nD: Even more complex - rotations in multiple planes simultaneously
- Key requirement: Rotations must form a finite group (discrete, not continuous)
Higher Dimensions:
- Direction representation:
- 2D:
[dx, dy]unit vector - 3D:
[dx, dy, dz]unit vector - nD:
[d1, d2, ..., dn]whered1² + d2² + ... + dn² = 1
- 2D:
- Rotation: n×n orthogonal matrices
- In our 2D solution, although we use simple pattern-based coordinate transformations to find the transformed (x,y), this is fundamentally a rotation problem around the origin that can be explained using linear algebra:
1 2 3 4 5 6 7 8 9 10 11# General rotation matrix for angle θ (counterclockwise): ⎡cos(θ) -sin(θ)⎤ ⎡x⎤ ⎡x·cos(θ) - y·sin(θ)⎤ ⎣sin(θ) cos(θ)⎦ ⎣y⎦ = ⎣x·sin(θ) + y·cos(θ)⎦ # For θ = 90° (Left turn): cos(90°)=0, sin(90°)=1 ⎡0 -1⎤ ⎡x⎤ ⎡-y⎤ ⎣1 0⎦ ⎣y⎦ = ⎣ x⎦ # For θ = -90° (Right turn): cos(-90°)=0, sin(-90°)=-1 ⎡ 0 1⎤ ⎡x⎤ ⎡ y⎤ ⎣-1 0⎦ ⎣y⎦ = ⎣-x⎦
- In our 2D solution, although we use simple pattern-based coordinate transformations to find the transformed (x,y), this is fundamentally a rotation problem around the origin that can be explained using linear algebra:
- Same logic: If direction changes after one cycle, robot forms closed path in finite steps
Variable Step Size:
- Replace
pos += direction * 1withpos += direction * step_length
3D Extension - Variable Step Lengths with X/Y/Z-Axis Rotations:
|
|
Key Insight: In any dimension with discrete rotations, the fundamental bounded condition remains the same regardless of step size.