12503 – Robot Instructions

12503 – Robot Instructions
Problem Link: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3947

 

You have a robot standing on the origin of x axis. The robot will be given some instructions. Your task is to predict its position after executing all the instructions.

LEFT : move one unit left (decrease p by 1, where p is the position of the robot before moving)

RIGHT : move one unit right (increase p by 1)

SAME AS i : perform the same action as in the i-th instruction. It is guaranteed that i is a positive integer not greater than the number of instructions before this.

Input

The first line contains the number of test cases T (T<=100).  Each test case begins with an integer n (1<=n<=100), the number of instructions. Each of the following n lines contains an instruction.

Output

For each test case, print the nal position of the robot. Note that after processing each test case, the

robot should be reset to the origin.

Sample Input

2
3
LEFT
RIGHT
SAME AS 2
5
LEFT
SAME AS 1
SAME AS 2
SAME AS 1
SAME AS 4

Sample Output

1
-5

 

//CODE Begins
//https://uva.onlinejudge.org/external/125/12503.pdf
//RUNTIME :  	0.000s

#include<cstdio>
#include<string>
#include<iostream>

using namespace std;

int main(){
	
	int n;
	
	scanf("%d",&n);
	
	string s,b;
	
	for(int i=1;i<=n;i++){
		
		int x;
		
		scanf("%d",&x);
		
		int arr[x+1];
		
		arr[0]=0;
		
		int c=0,y;
		
		for(int j=1;j<=x;j++){ cin >> s;
			
			if(s.compare("LEFT")==0){
				arr[j]=-1;
				c+=arr[j];
				}
			else if(s.compare("RIGHT")==0){
				arr[j]=1;
				c+=arr[j];				
				}
			else if(s.compare("SAME")==0){
				cin >> b >> y;
				arr[j]=arr[y];
				c+=arr[j];				
				}
			
			}
		cout << c << endl;
		
		}
	
	
	return 0;
	}
//CODE Ends

 

Advertisements