Class Solution
java.lang.Object
g1001_1100.s1041_robot_bounded_in_circle.Solution
1041 - Robot Bounded In Circle.<p>Medium</p>
<p>On an infinite plane, a robot initially stands at <code>(0, 0)</code> and faces north. Note that:</p>
<ul>
<li>The <strong>north direction</strong> is the positive direction of the y-axis.</li>
<li>The <strong>south direction</strong> is the negative direction of the y-axis.</li>
<li>The <strong>east direction</strong> is the positive direction of the x-axis.</li>
<li>The <strong>west direction</strong> is the negative direction of the x-axis.</li>
</ul>
<p>The robot can receive one of three instructions:</p>
<ul>
<li><code>"G"</code>: go straight 1 unit.</li>
<li><code>"L"</code>: turn 90 degrees to the left (i.e., anti-clockwise direction).</li>
<li><code>"R"</code>: turn 90 degrees to the right (i.e., clockwise direction).</li>
</ul>
<p>The robot performs the <code>instructions</code> given in order, and repeats them forever.</p>
<p>Return <code>true</code> if and only if there exists a circle in the plane such that the robot never leaves the circle.</p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> instructions = “GGLLGG”</p>
<p><strong>Output:</strong> true</p>
<p><strong>Explanation:</strong> The robot is initially at (0, 0) facing the north direction.</p>
<p>“G”: move one step. Position: (0, 1). Direction: North.</p>
<p>“G”: move one step. Position: (0, 2). Direction: North.</p>
<p>“L”: turn 90 degrees anti-clockwise. Position: (0, 2). Direction: West.</p>
<p>“L”: turn 90 degrees anti-clockwise. Position: (0, 2). Direction: South.</p>
<p>“G”: move one step. Position: (0, 1). Direction: South.</p>
<p>“G”: move one step. Position: (0, 0). Direction: South.</p>
<p>Repeating the instructions, the robot goes into the cycle: (0, 0) –> (0, 1) –> (0, 2) –> (0, 1) –> (0, 0).</p>
<p>Based on that, we return true.</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> instructions = “GG”</p>
<p><strong>Output:</strong> false</p>
<p><strong>Explanation:</strong> The robot is initially at (0, 0) facing the north direction.</p>
<p>“G”: move one step. Position: (0, 1). Direction: North.</p>
<p>“G”: move one step. Position: (0, 2). Direction: North.</p>
<p>Repeating the instructions, keeps advancing in the north direction and does not go into cycles.</p>
<p>Based on that, we return false.</p>
<p><strong>Example 3:</strong></p>
<p><strong>Input:</strong> instructions = “GL”</p>
<p><strong>Output:</strong> true</p>
<p><strong>Explanation:</strong> The robot is initially at (0, 0) facing the north direction.</p>
<p>“G”: move one step. Position: (0, 1). Direction: North.</p>
<p>“L”: turn 90 degrees anti-clockwise. Position: (0, 1). Direction: West.</p>
<p>“G”: move one step. Position: (-1, 1). Direction: West.</p>
<p>“L”: turn 90 degrees anti-clockwise. Position: (-1, 1). Direction: South.</p>
<p>“G”: move one step. Position: (-1, 0). Direction: South.</p>
<p>“L”: turn 90 degrees anti-clockwise. Position: (-1, 0). Direction: East.</p>
<p>“G”: move one step. Position: (0, 0). Direction: East.</p>
<p>“L”: turn 90 degrees anti-clockwise. Position: (0, 0). Direction: North.</p>
<p>Repeating the instructions, the robot goes into the cycle: (0, 0) –> (0, 1) –> (-1, 1) –> (-1, 0) –> (0, 0).</p>
<p>Based on that, we return true.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 100</code></li>
<li><code>instructions[i]</code> is <code>'G'</code>, <code>'L'</code> or, <code>'R'</code>.</li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
isRobotBounded
-