Expogo
Time limit20sMemory limit1024 MB
Find the shortest string of N, S, E, W jumps of length 1, 2, 4, and so on that lands exactly on (X, Y), or report IMPOSSIBLE.
- Level
Medium6 of 10
- Topics
- Math, Bit manipulation, Implementation
- Solved
- No attempts yet
Problem
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 standing at point in an infinite two-dimensional backyard. You want to reach a goal point , with integer coordinates, in as few jumps as possible. You must land exactly on the goal point. Passing over it during a jump is not enough.
Each jump, you pick a direction: north, south, east, or west. The -th jump moves you units in the chosen direction. The first jump moves 1 unit, the second moves 2 units, the third moves 4 units, and so on.
Determine whether the goal point can be reached. If it can, show how to reach it using as few jumps as possible.
Input
The first line contains the number of test cases . Each of the next lines contains two integers and , the coordinates of the goal point.
Output
For each test case, output one line Case #x: y, where is the test case number starting from 1. If the goal point cannot be reached, is IMPOSSIBLE. Otherwise, is a string of one or more characters from N, S, E, and W, giving the directions of the jumps in order. The jumps must land exactly on the goal point after the last jump, and the string must be as short as possible.
Constraints
.
Hint
In Sample 1, you can jump south from to , then east to , then north to . Two jumps or fewer are not enough. The goal is at least units away, but the first two jumps together cover only units.
Sample 2 is Sample 1 reflected across both axes, so its answer comes from reversing every direction in the answer to Sample 1.
In Sample 3, EWE reaches the target but is not a valid answer, because a shorter route exists.
Figure out why the target in Sample 4 cannot be reached.