#P13056. [GCJ 2020 #1B] Expogo

    ID: 12861 Type: RemoteJudge 20000ms 1024MiB Tried: 0 Accepted: 0 Difficulty: 4 Uploaded By: Tags>贪心2020Google Code Jam

[GCJ 2020 #1B] Expogo

题目描述

You have just received the best gift ever: an Expogo stick. You can stand on it and use it to make increasingly large jumps.

You are currently standing on point (0,0)(0,0) in your infinite two-dimensional backyard, and you are trying to reach a goal point (X,Y)(\mathrm{X}, \mathrm{Y}), with integer coordinates, in as few jumps as possible. You must land exactly on the goal point; it is not sufficient to pass over it on a jump.

Each time you use your Expogo stick to jump, you pick a cardinal direction: north, south, east, or west. The ii-th jump with your Expogo stick moves you 2(i1)2^{(i-1)} units in the chosen direction, so your first jump takes you 1 unit, your second jump takes you 2 units, your third jump takes you 4 units, and so on.

Given a goal point (X,Y)(\mathrm{X}, \mathrm{Y}), determine whether it is possible to get there, and if so, demonstrate how to do it using as few jumps as possible.

输入格式

The first line of the input gives the number of test cases, T\mathrm{T}. T\mathrm{T} test cases follow. Each consists of a single line with two integers X\mathrm{X} and Y\mathrm{Y} : the coordinates of the goal point.

输出格式

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 1) and yy is IMPOSSIBLE if the goal point cannot be reached. Otherwise, yy must be a string of one or more characters, each of which is either N\mathrm{N} (north), S\mathrm{S} (south), E\mathrm{E} (east), or W\mathrm{W} (west), representing the directions of the jumps that you will make, in order. This sequence of jumps must reach the goal point at the end of the final jump, and it must be as short as possible.

4
2 3
-2 -3
3 0
-1 1
Case #1: SEN
Case #2: NWS
Case #3: EE
Case #4: IMPOSSIBLE

提示

Sample Explanation

In Sample Case #1, you can jump south from (0,0)(0, 0) to (0,1)(0, -1), then jump east to (2,1)(2, -1), then jump north to (2,3)(2, 3).

We can be sure there is not a more efficient solution (two moves or fewer) because at least 2+3=52 + 3 = 5 units of distance are needed to reach the goal point, but the first two jumps combined only give us 33 units of distance.

Notice that Sample Case #2 is like Sample Case #1 but reflected across both axes, and so the answer comes from reflecting all directions in Sample Case #1's answer.

In Sample Case #3, notice that EWE would not be a valid answer, even though it reaches the target, because there is a way to get there using fewer jumps.

We leave it to you to determine why it is impossible to reach the target in Sample Case #4.

Limits

  • (X,Y)(0,0)(\text{X}, \text{Y}) \neq (0, 0).

Test set 1 (5 Pts, Visible Verdict)

  • 1T801 \leqslant \text{T} \leqslant 80.
  • 4X4-4 \leqslant \text{X} \leqslant 4.
  • 4Y4-4 \leqslant \text{Y} \leqslant 4.

Test set 2 (8 Pts, Visible Verdict)

  • 1T1001 \leqslant \text{T} \leqslant 100.
  • 100X100-100 \leqslant \text{X} \leqslant 100.
  • 100Y100-100 \leqslant \text{Y} \leqslant 100.

Test set 3 (16 Pts, Visible Verdict)

  • 1T1001 \leqslant \text{T} \leqslant 100.
  • 109X109-10^{9} \leqslant \text{X} \leqslant 10^{9}.
  • 109Y109-10^{9} \leqslant \text{Y} \leqslant 10^{9}.