topcoder problem BearNSWE submitted by zig_zag
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <string>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
double xx, yy;
class BearNSWE
{
public:
double totalDistance(vector <int> a, string dir)//no main function
{
int n = a.size();
double ans = 0.0;
xx = yy = 0.0;
for (int i = 0; i < n; i++)
{
if (dir[i] == 'N')
{
yy = yy + a[i];
}
else if (dir[i] == 'S')
{
yy = yy - a[i];
}
else if (dir[i] == 'W')
{
xx = xx - a[i];
}
else if (dir[i] == 'E')
{
xx = xx + a[i];
}
ans = ans + a[i];
}
return ans + sqrt(xx*xx + yy*yy);
}
};
The topcoder runner expects a class with a specific method interface, it is specified in the problem statement. For this specific problem, it is here.
Topcoder will compile this code with additional sources - a main method, which runs the examples - added.
If you want to test your code locally in the topcoder editor (or just autogenerate the code for running it), there are several plugins available for it, for example the ExampleBuilder.