#include<bits/stdc++.h>

using namespace std;

string rec(string ans, int x, int y){
if(x + y == 0){
return "";
}
if(x == 1 && y == 0) return ans + 'E';
if(x == -1 && y == 0) return ans + 'W';
if(x == 0 && y == 1) return ans + 'N';
if(x == 0 && y ==  -1) return ans + 'S';
if(x % 2 == 0){
if((((y + 1) + x) / 2) % 2 == 1) return ans + 'S' + rec(ans, x/2, (y + 
1)/2);

if((((y - 1) + x) / 2) % 2 == 1) return ans + 'N' + rec(ans, x/2, (y - 
1)/2);
}
if(y % 2 == 0){
if((((x + 1) + y) / 2) % 2 == 1) return ans + 'W' + rec(ans, (x + 1)/2, 
y/2);
if((((x - 1) + y) / 2) % 2 == 1) return ans + 'E' + rec(ans, (x - 1)/2, 
y/2);
}
}

void solve(){
int xx, yy;
cin>>xx>>yy;
int x = abs(xx);
int y = abs(yy);
int total = x + y;
if(total % 2 == 0){cout<<"IMPOSSIBLE"<<endl;return;}
string a = rec(a, abs(x), abs(y));
if(xx < 0 && yy < 0){
string opp = "";
for(char c: a){
if(c == 'N') opp += 'N';
if(c == 'S') opp += 'S';
if(c == 'E') opp += 'E';
if(c == 'W') opp += 'W';
}
a  = opp;
}
if(xx < 0 && yy < 0){
string opp = "";
for(char c: a){
if(c == 'N') opp += 'S';
if(c == 'S') opp += 'N';
if(c == 'E') opp += 'W';
if(c == 'W') opp += 'E';
}
a  = opp;
}
if(xx < 0 && yy > 0){
string opp = "";
for(char c: a){
if(c == 'N') opp += 'N';
if(c == 'S') opp += 'S';
if(c == 'E') opp += 'W';
if(c == 'W') opp += 'E';
}
a  = opp;
}
if(xx > 0 && yy < 0){
string opp = "";
for(char c: a){
if(c == 'N') opp += 'S';
if(c == 'S') opp += 'N';
if(c == 'E') opp += 'E';
if(c == 'W') opp += 'W';
}
a  = opp;
}
cout<<a<<endl; 
}
int main(){
int t;
cin>>t;
for(int tt = 1; tt<=t; tt++){
cout<<"Case #"<<tt<<": ";
solve();
}
return 0;
}

-- 
You received this message because you are subscribed to the Google Groups 
"Google Code Jam" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to google-code+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-code/ce673dd5-2840-44fa-aa86-aaa4a876fa88%40googlegroups.com.

Reply via email to