Does robot moves circular
PROBLEM :
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
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is a String in capital letter, sequence of moves consisting only {R,G,L}.
Output:
Print Given sequence of moves is circular if first and last positions of robot are same. else Given sequence of moves is NOT circular.
Constraints:
1 = T = 50
1 = size of string = 200
Example:
Input:
3
GLGLGLG
GLLG
GGGGL
Output:
Circular
Circular
Not Circular
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#define N 0
#define E 1
#define S 2
#define W 3
int robot(string str)
{
int x=0,y=0 ;
int dir=N ;
for(int i=0;i<str.length();i++)
{
if(str[i]=='R')
dir=(dir+1)%4;
else if(str[i]=='L')
dir=(4+dir-1)%4;
else
{
if(dir==N)
y++ ;
else if(dir==E)
x++ ;
else if(dir==S)
y-- ;
else
x-- ;
}
}
return (x==0&&y==0) ;
}
int main()
{
int t ;
cin>>t ;
while(t--)
{
string str ;
cin>>str ;
if(robot(str))
cout<<"Circular" ;
else
cout<<"Not Circular" ;
cout<<endl ;
}
return 0;
}
---------------------------------------------------------------------------------
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
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is a String in capital letter, sequence of moves consisting only {R,G,L}.
Output:
Print Given sequence of moves is circular if first and last positions of robot are same. else Given sequence of moves is NOT circular.
Constraints:
1 = T = 50
1 = size of string = 200
Example:
Input:
3
GLGLGLG
GLLG
GGGGL
Output:
Circular
Circular
Not Circular
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#define N 0
#define E 1
#define S 2
#define W 3
int robot(string str)
{
int x=0,y=0 ;
int dir=N ;
for(int i=0;i<str.length();i++)
{
if(str[i]=='R')
dir=(dir+1)%4;
else if(str[i]=='L')
dir=(4+dir-1)%4;
else
{
if(dir==N)
y++ ;
else if(dir==E)
x++ ;
else if(dir==S)
y-- ;
else
x-- ;
}
}
return (x==0&&y==0) ;
}
int main()
{
int t ;
cin>>t ;
while(t--)
{
string str ;
cin>>str ;
if(robot(str))
cout<<"Circular" ;
else
cout<<"Not Circular" ;
cout<<endl ;
}
return 0;
}
---------------------------------------------------------------------------------
Comments
Post a Comment