Check if a given sequence of moves for a robot is circular or not

Given a sequence of moves for a robot, check if the sequence is circular or not. A sequence of moves is circular if first and last positions of robot are same. A move can be on of the following.
G – Go one unit
L – Turn left
R – Turn right
Examples:
Input: path[] = "GLGLGLG"
Output: Given sequence of moves is circular

Input: path[] = "GLLG"
Output: Given sequence of moves is circular

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = "GRGLGLGGGLGLGRGLG";
            int x = 0, y = 0;
            int direction = 1;// 1 --> N; 2 --> W; 3 --> S; 4 --> E
            //start the traversal
            for(int i = 0; i < path.Length; i++)
            {
                //check for the direction first and set the orientation
                if (path[i] == 'L')
                {
                    if(direction <= 3)
                    {
                        direction++;
                    }
                    else
                    {
                        direction = 1;
                    }
                }
                else if (path[i] == 'R')
                {
                    if (direction > 1)
                    {
                        direction--;
                    }
                    else
                    {
                        direction = 4;
                    }
                }
                else //make the move
                {
                    if (direction == 1)
                    {
                        y++;
                    }
                    else if (direction == 3)
                    {
                        y--;
                    }
                    else if (direction == 2)
                    {
                        x--;
                    }
                    else
                    {
                        x++;
                    }
                }
            }
            if(path.Length != 0 && path.Contains("G") && x == 0 && y == 0)
            {
                Console.WriteLine("The path traversal is cyclic");
            }
            else
            {
                Console.WriteLine("The path traversal is non-cyclic");
            }
        }
    }
}

Output
=======
The path traversal is cyclic
Press any key to continue . . .
Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s