Check if given set of moves is circular or not

Check if given set of moves is circular or not. The move is circular if its starting and ending coordinates are the same. The moves can contain instructions to move one unit in same direction (M), to change direction to left of current direction (L) and to change direction to right of current direction (R).


 

For example,


Set of moves MRMRMRM is circular
Set of moves MRMLMRMRMMRMM is circular

 
The idea is very simple. We start with (0, 0) as starting coordinates and North as starting direction and linearly read each instruction from the input string. For every instruction, we either update the coordinates of the current location (x, y) if the instruction is MOVE or we update the current direction if the instruction is GO LEFT or GO RIGHT. If in the end we’re back to starting coordinates (0, 0), the move is circular.

 
Below is C++ and Java implementation of the idea –

C++

Download   Run Code

Java

Download   Run Code

Output:

Circular Move

 

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading...

 
Thanks for reading.

Please use our online compiler to post code in comments. To contribute, get in touch with us.
Like us? Please spread the word and help us grow. Happy coding 🙂


Leave a Reply

avatar
  Subscribe  
newest oldest most voted
Notify of
Abhi shyam
Guest

Please try to provide some more explanation for the problem statement