Skip to content

Instantly share code, notes, and snippets.

@naoyat
Last active December 17, 2015 06:19
Show Gist options
  • Save naoyat/5564233 to your computer and use it in GitHub Desktop.
Save naoyat/5564233 to your computer and use it in GitHub Desktop.
Problem B #Round1C
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <vector>
using namespace std;
#define all(c) (c).begin(),(c).end()
#define repN(var,n) for(int var=1;var<=(n);var++)
inline int a(int x, int y) {
return abs(x) + abs(y);
}
string solve(int X, int Y) {
int axy = a(X,Y);
int m_max = axy*2;
int m_min = sqrt(axy*2) - 1;
for (int m=m_min; m<=m_max; ++m) {
vector<char> cs;
int x = X, y = Y;
for (int v=m; v>=1; --v) {
if (a(v-abs(x),y) < a(x,v-abs(y))) {
if (x > 0) {
cs.push_back('E');
x -= v;
} else {
cs.push_back('W');
x += v;
}
} else {
if (y > 0) {
cs.push_back('N');
y -= v;
} else {
cs.push_back('S');
y += v;
}
}
}
if (a(x,y) == 0) {
reverse(all(cs));
return string(all(cs));
}
}
return "?";
}
main(){
int _T; cin>>_T;
repN(_t,_T) {
int X, Y; cin >> X >> Y;
cout << "Case #" << _t << ": " << solve(X,Y) << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment