
Problem Statement
In this problem, we are dealing with a robotic simulation on an infinite plane. The robot starts at position (0, 0) facing north, and executes a sequence of instructions that include moving forward by one unit ('G'), turning left ('L'), and turning right ('R'). After executing all the given instructions, it repeats the entire sequence indefinitely. The task is to determine whether the robot's movement is confined to a certain circle, meaning it eventually returns to a starting point and continues to loop from there. If the robot's route brings it back to any prior position, facing the same direction as before, it suggests a cyclic pattern, and we should return true
. Otherwise, if the robot continues moving away and never loops, return false
.
Examples
Example 1
Input:
instructions = "GGLLGG"
Output:
true
Explanation:
The robot is initially at (0, 0) facing the north direction. "G": move one step. Position: (0, 1). Direction: North. "G": move one step. Position: (0, 2). Direction: North. "L": turn 90 degrees anti-clockwise. Position: (0, 2). Direction: West. "L": turn 90 degrees anti-clockwise. Position: (0, 2). Direction: South. "G": move one step. Position: (0, 1). Direction: South. "G": move one step. Position: (0, 0). Direction: South. Repeating the instructions, the robot goes into the cycle: (0, 0) --> (0, 1) --> (0, 2) --> (0, 1) --> (0, 0). Based on that, we return true.
Example 2
Input:
instructions = "GG"
Output:
false
Explanation:
The robot is initially at (0, 0) facing the north direction. "G": move one step. Position: (0, 1). Direction: North. "G": move one step. Position: (0, 2). Direction: North. Repeating the instructions, keeps advancing in the north direction and does not go into cycles. Based on that, we return false.
Example 3
Input:
instructions = "GL"
Output:
true
Explanation:
The robot is initially at (0, 0) facing the north direction. "G": move one step. Position: (0, 1). Direction: North. "L": turn 90 degrees anti-clockwise. Position: (0, 1). Direction: West. "G": move one step. Position: (-1, 1). Direction: West. "L": turn 90 degrees anti-clockwise. Position: (-1, 1). Direction: South. "G": move one step. Position: (-1, 0). Direction: South. "L": turn 90 degrees anti-clockwise. Position: (-1, 0). Direction: East. "G": move one step. Position: (0, 0). Direction: East. "L": turn 90 degrees anti-clockwise. Position: (0, 0). Direction: North. Repeating the instructions, the robot goes into the cycle: (0, 0) --> (0, 1) --> (-1, 1) --> (-1, 0) --> (0, 0). Based on that, we return true.
Constraints
1 <= instructions.length <= 100
instructions[i]
is'G'
,'L'
or,'R'
.
Approach and Intuition
Simulation of Movements: The robot performs a sequence of moves based on the instructions, and we simulate each movement and turn. We record the robot's position and direction after each instruction.
Tracking Position and Direction:
- The robot has four possible directions (north, south, east, west), which we can represent with direction changes on the Cartesian plane. East and north can be positive axes, while west and south are negative axes on the plane.
- Moving forward ('G') impacts the position based on the current direction. Turning left ('L') or right ('R') changes the direction but not the position.
Determining Cycles: We understand from the examples that returning to a previous position with the same facing direction indicates a repeating pattern or cycle.
- For example, the instruction set "GL" makes the robot turn in a square, bringing it back to the original position (0, 0) facing north, hence forming a loop.
- Conversely, a sequence like "GG" on repeat simply moves the robot farther north with no looping behavior.
Result Computation: Using the logic, if during the simulation the robot returns to any previously visited position, facing the same direction as before, we can conclude it's a loop and return
true
. If the loop completes without repetition, and if extended movement doesn't suggest an upcoming loop, the result would befalse
.
By following these steps, we can efficiently determine if the robot's movement sequence results in a cyclic pattern, thus solving the problem based on the repeated movement simulation and tracking.
Solutions
- Java
class Solution {
public boolean isRobotBounded(String instructions) {
int[][] move = new int[][]{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int xPosition = 0, yPosition = 0;
int direction = 0;
for (char instruction : instructions.toCharArray()) {
if (instruction == 'L')
direction = (direction + 3) % 4;
else if (instruction == 'R')
direction = (direction + 1) % 4;
else {
xPosition += move[direction][0];
yPosition += move[direction][1];
}
}
return (xPosition == 0 && yPosition == 0) || (direction != 0);
}
}
The provided Java solution resolves the problem of determining if a robot is bounded within a circle after executing a series of instructions. This problem uses the isRobotBounded
method, which takes a string of instructions and analyzes the robot's movements based on those directives. Here's how the solution functions:
- An array named
move
is initialized to represent the movement directions - North, East, South, and West. - Initial position of the robot is set at
(0, 0)
with direction facing North. - The code iterates over each character in the
instructions
string. Depending on the character:- 'L' turns the robot left (counter-clockwise).
- 'R' turns the robot right (clockwise).
- 'G' moves the robot one unit forward in its current direction.
- The direction of the robot is managed by a variable
direction
, which cyclically adjusts based on the modulo operation. - After processing all instructions, the robot's final position
(xPosition, yPosition)
is checked:- If the robot returns to the origin
(0,0)
or is not facing North, it's bounded within a circle, returningtrue
. - Otherwise, it returns
false
, indicating it is not circled bounded.
- If the robot returns to the origin
This logic ensures that the method can assess whether repeating the series of instructions indefinitely will confine the robot within a finite area or not, effectively resolving the problem.
- Python
class Solution:
def isRobotBounded(self, commands: str) -> bool:
# Define the four cardinal directions
direction_vectors = [[0, 1], [1, 0], [0, -1], [-1, 0]]
# Initialize coordinates
x_coord = y_coord = 0
# Start facing north
direction_index = 0
for command in commands:
if command == "L":
direction_index = (direction_index - 1) % 4
elif command == "R":
direction_index = (direction_index + 1) % 4
else:
x_coord += direction_vectors[direction_index][0]
y_coord += direction_vectors[direction_index][1]
# Check if robot is back at the origin or not facing north
return (x_coord == 0 and y_coord == 0) or direction_index != 0
This solution determines whether a robot, given a sequence of commands ('G' for move forward, 'L' for turn left, 'R' for turn right), ends up in a circle, meaning it returns to its starting point or not facing the original direction after executing the commands. The function isRobotBounded
operates by applying the instructions to alter the robot's position and orientation.
- Initialize
direction_vectors
to represent north, east, south, and west respectively. - Set
x_coord
andy_coord
to zero, representing the robot's starting position. - Set
direction_index
to zero, indicating the robot starts facing north.
Proceed through each command in the sequence:
- On 'L', turn left by decrementing
direction_index
and compute modulo 4 to wrap around. - On 'R', turn right by incrementing
direction_index
and compute modulo 4 similarly. - On 'G', move forward in the current direction by adding the appropriate values from
direction_vectors
tox_coord
andy_coord
.
After processing all commands, return True
if the robot is back at the origin (x_coord == 0 and y_coord == 0
) or if it is not facing north (direction_index != 0
). Otherwise, return False
, indicating the robot does not end up in a circle. This algorithm efficiently checks the robot's final state using a finite set of operations relative to the number of commands, leading to a straightforward and practical implementation.
No comments yet.