Guest User

Google Code Jam 2013 Round 1C, Task B

a guest
May 12th, 2013
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #include<cstdio>
  2. #include<algorithm>
  3. #include<string>
  4. #include<iostream>
  5.  
  6. using namespace std;
  7.  
  8. int T, x, y, m, l;
  9. string s;
  10.  
  11. void foo (int x, int y, int ans)
  12. {
  13.     if (ans == 0)
  14.         return;
  15.     int m1 = abs(x - ans) + abs(y);
  16.     int m2 = abs(x + ans) + abs(y);
  17.     int m3 = abs(x) + abs(y - ans);
  18.     int m4 = abs(x) + abs(y + ans);
  19.     if (m1 <= m2 && m1 <= m3 && m1 <= m4)
  20.     {
  21.         s += 'E';
  22.         foo (x - ans, y, ans - 1);
  23.     }
  24.     if (m2 < m1 && m2 <= m3 && m2 <= m4)
  25.     {
  26.         s += 'W';
  27.         foo (x + ans, y, ans - 1);
  28.     }
  29.     if (m3 < m1 && m3 < m2 && m3 <= m4)
  30.     {
  31.         s += 'N';
  32.         foo (x, y - ans, ans - 1);
  33.     }
  34.     if (m4 < m1 && m4 < m2 && m4 < m3)
  35.     {
  36.         s += 'S';
  37.         foo (x, y + ans, ans - 1);
  38.     }
  39. }
  40.  
  41. int main()
  42. {
  43.     freopen("input.txt", "r", stdin);
  44.     freopen("output.txt", "w", stdout);
  45.     scanf("%d", &T);
  46.     for (int g = 0; g < T; g++)
  47.     {
  48.         s = "";
  49.         printf("Case #%d: ", g + 1);
  50.         scanf("%d%d", &x, &y);
  51.         m = abs(x) + abs(y);
  52.         for (int i = 1; i < 10000; i++)
  53.             if (i * (i + 1) / 2 >= m)
  54.             {
  55.                 l = i;
  56.                 break;
  57.             }
  58.         while ((l * (l + 1) / 2) % 2 != m % 2)
  59.             l++;
  60.         foo(x, y, l);
  61.         reverse(s.begin(), s.end());
  62.         cout << s << endl;
  63.     }
  64.     return 0;
  65. }
Add Comment
Please, Sign In to add comment