1041. Robot Bounded In Circle Medium

@problem@discussion
#Math#String#Simulation



1/**
2 * [1041] Robot Bounded In Circle
3 *
4 * On an infinite plane, a robot initially stands at (0, 0) and faces north. Note that:
5 * 
6 * 	The north direction is the positive direction of the y-axis.
7 * 	The south direction is the negative direction of the y-axis.
8 * 	The east direction is the positive direction of the x-axis.
9 * 	The west direction is the negative direction of the x-axis.
10 * 
11 * The robot can receive one of three instructions:
12 * 
13 * 	"G": go straight 1 unit.
14 * 	"L": turn 90 degrees to the left (i.e., anti-clockwise direction).
15 * 	"R": turn 90 degrees to the right (i.e., clockwise direction).
16 * 
17 * The robot performs the instructions given in order, and repeats them forever.
18 * Return true if and only if there exists a circle in the plane such that the robot never leaves the circle.
19 *  
20 * Example 1:
21 * 
22 * Input: instructions = "GGLLGG"
23 * Output: true
24 * Explanation: The robot is initially at (0, 0) facing the north direction.
25 * "G": move one step. Position: (0, 1). Direction: North.
26 * "G": move one step. Position: (0, 2). Direction: North.
27 * "L": turn 90 degrees anti-clockwise. Position: (0, 2). Direction: West.
28 * "L": turn 90 degrees anti-clockwise. Position: (0, 2). Direction: South.
29 * "G": move one step. Position: (0, 1). Direction: South.
30 * "G": move one step. Position: (0, 0). Direction: South.
31 * Repeating the instructions, the robot goes into the cycle: (0, 0) --> (0, 1) --> (0, 2) --> (0, 1) --> (0, 0).
32 * Based on that, we return true.
33 * 
34 * Example 2:
35 * 
36 * Input: instructions = "GG"
37 * Output: false
38 * Explanation: The robot is initially at (0, 0) facing the north direction.
39 * "G": move one step. Position: (0, 1). Direction: North.
40 * "G": move one step. Position: (0, 2). Direction: North.
41 * Repeating the instructions, keeps advancing in the north direction and does not go into cycles.
42 * Based on that, we return false.
43 * 
44 * Example 3:
45 * 
46 * Input: instructions = "GL"
47 * Output: true
48 * Explanation: The robot is initially at (0, 0) facing the north direction.
49 * "G": move one step. Position: (0, 1). Direction: North.
50 * "L": turn 90 degrees anti-clockwise. Position: (0, 1). Direction: West.
51 * "G": move one step. Position: (-1, 1). Direction: West.
52 * "L": turn 90 degrees anti-clockwise. Position: (-1, 1). Direction: South.
53 * "G": move one step. Position: (-1, 0). Direction: South.
54 * "L": turn 90 degrees anti-clockwise. Position: (-1, 0). Direction: East.
55 * "G": move one step. Position: (0, 0). Direction: East.
56 * "L": turn 90 degrees anti-clockwise. Position: (0, 0). Direction: North.
57 * Repeating the instructions, the robot goes into the cycle: (0, 0) --> (0, 1) --> (-1, 1) --> (-1, 0) --> (0, 0).
58 * Based on that, we return true.
59 * 
60 *  
61 * Constraints:
62 * 
63 * 	1 <= instructions.length <= 100
64 * 	instructions[i] is 'G', 'L' or, 'R'.
65 * 
66 */
67pub struct Solution {}
68
69// problem: https://leetcode.com/problems/robot-bounded-in-circle/
70// discuss: https://leetcode.com/problems/robot-bounded-in-circle/discuss/?currentPage=1&orderBy=most_votes&query=
71
72// submission codes start here
73
74impl Solution {
75    pub fn is_robot_bounded(instructions: String) -> bool {
76        false
77    }
78}
79
80// submission codes end
81
82#[cfg(test)]
83mod tests {
84    use super::*;
85
86    #[test]
87    fn test_1041() {
88    }
89}
90


Back
© 2025 bowen.ge All Rights Reserved.