Testing Robot : TSTROBOT
Testing Robot Codechef Solution
Public Submission link: Click here (anyone can submit here )
CU Submission link: Click here (need login by CU account)
Read problems statements Mandarin , Bengali , Hindi , Russian and Vietnamese as well.
Chef has bought a new robot, which will be used for delivering dishes to his customers. He started testing the robot by letting it move on a line.
Initially, the robot is placed at the coordinate x=Xx=X. Then, it should execute a sequence of NN commands, described by a string SS with length NN. Each character of this string is either ‘L’ or ‘R’, denoting that the robot should walk one step to the left (decreasing xx by 11) or to the right (increasing xx by 11), respectively.
How many distinct points are visited by the robot when it has executed all the commands? A point pp is visited by the robot if pp is an integer and the robot’s position before or after executing some command is x=px=p.
Input
- The first line of the input contains a single integer TT denoting the number of test cases. The description of TT test cases follows.
- The first line of each test case contains two space-separated integers NN and XX.
- The second line contains a single string SS with length NN.
Output
For each test case, print a single line containing one integer ― the number of points visited by the robot.
Constraints
- 1≤T≤1001≤T≤100
- 1≤N≤1001≤N≤100
- |X|≤1,000,000|X|≤1,000,000
- SS contains only characters ‘L’ and ‘R’
Subtasks
Subtask #1 (100 points): original constraints
Sample Input 1
2
6 10
RRLLLL
2 0
LL
Sample Output 1
5
3
Explanation
Example case 1: The robot followed the path 10→11→12→11→10→9→810→11→12→11→10→9→8.
Example case 2: The robot followed the path 0→−1→−20→−1→−2.
Testing Robot Codechef Solution in C
int main(void) {
int t,n,size;
unsigned long x;
scanf("%d",&t);
while(t--){
int index=0;
size=0;
scanf("%d %u",&n,&x);
char str[n+1];
unsigned long temp[n+1];
temp[0]=x;
size=1;
scanf("%s",str);
while(index < n){
if(str[index]=='L')
x=x-1;
else
x=x+1;
size=insert(temp,size,x);
index++;
}
printf("%d\n",size);
}
return 0;
}
int insert(unsigned long array[],int size,unsigned long data){
int index=0;
while(index < size){
if(array[index]==data)
return size;
index++;
}
array[size]=data;
return ++size;
}
Testing Robot Codechef Solution in C++ 14
int main() {
int t;
cin>>t;
while(t--)
{
int n,x;
cin>>n>>x;
string s;
cin>>s;
int max=x;
int min =x;
for (int i = 0; i < n; i++)
{
if(s[i]=='R')
{
x++;
if(x>max)
{
max=x;
}
}
else
{
x--;
if(x < min)
{
min=x;
}
}
}
cout << max-min+1;
cout << endl;
}
return 0;
}
Testing Robot Codechef Solution in Python 3
from collections import Counter
for _ in range(int(input())):
c, s = map(int, input().split())
p = input()
q = {s}
for i in p:
if i == 'R':
q.add(s:=s+1)
else:
q.add(s:=s-1)
print(len(q))
Testing Robot Codechef Solution in Java
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
int x = sc.nextInt();
String s = sc.next();
int a = x, b=x;
for(int i=0;i<n;i++){
if(s.charAt(i)=='R'){
x++;
}
else{
x--;
}
if(x>a){
a=x;
}
if(x < b){
b=x;
}
}
System.out.println(a-b+1);
}
}
}