A Die Maker
Time limit8sMemory limit256 MB
Roll a die on a board so each move increments the face that lands down, and print the dictionary-smallest move string that reaches the six target numbers.
- Level
Hard8 of 10
- Topics
- BFS, Greedy, Backtracking, Simulation
- Solved
- No attempts yet
Problem
A die maker's day starts early in the morning.
You are a die maker. You take orders from customers and make many kinds of dice every day. Today's order is a cubic die with the six numbers written one per face. It does not matter which number goes on which face.
You make the die on a tool shaped like a flat board. You start with a die that has a zero on every face, resting on the tool. When you rotate the die by 90 degrees on the tool toward the north, the south, the east, or the west, the number on the face that newly touches the tool grows by one. By rotating the die toward suitable directions again and again, you can obtain the ordered die.
The final number on each face is decided by the sequence of directions you rotate the die toward. The string that represents that sequence of directions is called an operation sequence. Formally, an operation sequence consists of characters, where is the number of rotations made. If the -th rotation is eastward, the -th character of the operation sequence is E. In the same way it is W for westward, S for southward, and N for northward. For example, the operation sequence NWS represents three rotations, northward first, westward next, and southward last.
Given the six integers of a customer's order, compute an operation sequence that makes the ordered die. If two or more operation sequences are possible, compute the earliest one in dictionary order.
Input
The input consists of multiple datasets. The number of datasets does not exceed 40. Each dataset has the following form.
are the integers of the customer's order. and are positive integers that specify the part of the operation sequence to print, and the output section gives the details.
Each dataset satisfies and . A line containing six zeros denotes the end of the input.
Output
For each dataset, print on one line the characters from position to position , both included, of the operation sequence that is the earliest in dictionary order. If the ordered die cannot be made, print impossible.
Dictionary order is defined as follows. The empty string comes first. For two nonempty strings and , the string precedes the string in dictionary order if
- precedes in alphabetical order, from 'A' to 'Z', or
- and are the same character and precedes in dictionary order.