Robot Bounded In Circle
In this problem, we want to determine whether a robot, following a given sequence of instructions repeatedly, will move in a bounded circle.
Solution
To find if the robot is bounded by a circle, we need to observe if it eventually returns to the starting position or faces any other direction except north after executing the instructions once. If either of these conditions is met, it is guaranteed that repeating the instructions will keep the robot bounded in a circle.
|
|
Explanation
direction
represents the current facing direction of the robot, with0
for north,1
for east,2
for south, and3
for west.moves
is a list containing the changes inx
andy
coordinates corresponding to each direction.- We iterate through the instructions and update the robot’s position and direction accordingly.
- Finally, we check if the robot has returned to the starting position
(0, 0)
or if it faces a direction other than north. If either condition is met, we returnTrue
, meaning the robot is bounded in a circle. Otherwise, we returnFalse
.
The time complexity of this solution is O(n), where n is the length of the instructions, and the space complexity is O(1).