Math     Medium     Simulation     String    

Problem Statement:

On an infinite plane, a robot initially stands at (0, 0) and faces north. Note that:

  • The north direction is the positive direction of the y-axis.
  • The south direction is the negative direction of the y-axis.
  • The east direction is the positive direction of the x-axis.
  • The west direction is the negative direction of the x-axis.

The robot can receive one of three instructions:

  • "G": go straight 1 unit.
  • "L": turn 90 degrees to the left (i.e., anti-clockwise direction).
  • "R": turn 90 degrees to the right (i.e., clockwise direction).

The robot performs the instructions given in order, and repeats them forever.

Return true if and only if there exists a circle in the plane such that the robot never leaves the circle.

 

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'.

Solution:

After little bit of experimenting on paper, one can realize that the only situation that it does not land up at origin is if we do not end up on origin after one round and are also facing north. The other two cases are:

  • Ending up on origin: Obviously it is true
  • Not ending up on origin but facing South/East/West: This is also true because after 2 rounds(South) or 4 rounds(East/West), we will again land at origin.

Now for implementation, we will use three variables:

  • a for x-coordinate
  • b for y-coordinate
  • c for direction

All 3 are integers. One important thing to notice is that we can use c variable for direction as follows:

  • If you see ‘L’, increment c by 1
  • If you see ‘R’, increment c by 3
  • If you see ‘G’, do not change c

Further we can check which direction we are facing:

  • c%4=0 means North
  • c%4=1 means West
  • c%4=2 means South
  • c%4=3 means East

Now the logic is that:

  • If you see G, find out which direction you are facing and modify the coordinates accordingly. This will change either a or b.
  • If you see L or R, change the variable c as per rule given above. Variables a and b do not change in this case.
class Solution {
public:
    bool isRobotBounded(string instructions) {
        int a=0,b=0,c=0;
        for(char ch: instructions)
        {
            if(ch=='G')
            {
                if(c%4==0)b++;
                if(c%4==1)a--;
                if(c%4==2)b--;
                if(c%4==3)a++;
            }
            if(ch=='L') c++;
            if(ch=='R') c+=3;
        }
        cout << a << ' ' << b << ' ' << c << endl;
        if(c%4==0 && (a!=0 || b!=0)) return false;
        return true;
    }
};