-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathDistance and Displacement
More file actions
48 lines (40 loc) · 936 Bytes
/
Distance and Displacement
File metadata and controls
48 lines (40 loc) · 936 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// { Driver Code Starts
// Initial Template for C++
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
// User function Template for C++
class Solution{
public:
int distance(int n, int a[], char d[]){
// code here
int x=0,y=0,z=0;
for(int i=0;i<n;i++){
if(d[i]=='N'){x+=a[i];}
else if(d[i]=='S'){x-=a[i];}
else if(d[i]=='E'){y-=a[i];}
else if(d[i]=='W'){y+=a[i];}
z+=a[i];
}
z += ceil(sqrt(x*x + y*y));
return z;
}
};
// { Driver Code Starts.
int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int a[n];
char d[n];
for(int i = 0;i < n;i++)
cin>>a[i];
for(int i = 0;i < n;i++)
cin>>d[i];
Solution ob;
cout<<ob.distance(n, a, d)<<"\n";
}
return 0;
} // } Driver Code Ends