Problem Statement
Contest Source
Author, Tester and Editorialist : Rohail Alam
DIFFICULTY:
Simple
PREREQUISITES:
None
PROBLEM:
Given a string, you are supposed to find out the value of two numbers (which denote the position of an entity) depending on the values of each of its characters.
EXPLANATION
To solve this problem, you traverse through each character of a given string and increment/decrement your x-coordinate and y-coordinate values depending on the character being looked at. From the problem statement, you can say that if the current character is :
- ’ U ’ - Increment the y-coordinate.
- ’ D ’ - Decrement the y-coordinate.
- ’ R ’ - Increment the x-coordinate.
- ’ L ’ - Decrement the y-coordinate.
After this is done, all you’ll need to do then is to print the x and the y coordinates separated by a space.
SOLUTION
C++ Solution
#include<bits/stdc++.h>
using namespace std;
int main(){
map<char,pair<int,int>> vals;
vals['U'] = {0,1};
vals['D'] = {0,-1};
vals['R'] = {1,0};
vals['L'] = {-1,0};
int tt;
cin>>tt;
while(tt--){
int n;
cin>>n;
string s;
cin>>s;
int xpos = 0, ypos = 0;
for(int i=0;i<n;++i){
xpos+=vals[s[i]].first;
ypos+=vals[s[i]].second;
}
cout<<xpos<<" "<<ypos<<endl;
}
}