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

 

Thanks for reading.

Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂

Get great deals at Amazon




Leave a Reply

avatar
  Subscribe  
newest oldest most voted
Notify of
Abhi shyam
Guest
Abhi shyam

Please try to provide some more explanation for the problem statement