eolymp
bolt
Try our new interface for solving problems
Problems

Division

Division

Two mining companies found together a good spot for gold mining. Now, tough negotiations are coming: the plot of land needs to be split between the companies. The terrain is square-shaped, and has been divided into \textbf{n} * \textbf{n} unit squares. Every small square is either empty, or contains one or two deposits of gold. Your goal is to split the plot into two connected parts, not necessarily identical or of the same area. The simplest propositions are what the companies like best. Therefore, the border between their areas has to start in northwest corner, end in southeast corner, and must go along the unit squares' borders. Furthermore, the length of the border must not exceed \textbf{2n}. Of course, the two parts must contain exactly the same number of gold deposits. \InputFile The first line contains the number of test cases \textbf{t}. The test cases follow. The first line of a test case contains a single integer \textbf{n}, the length of the side of the plot (\textbf{1} ≤ \textbf{n} ≤ \textbf{1000}). The next \textbf{n} lines contain the description of the plot: \textbf{n} rows containing \textbf{n} integers each. These are the numbers of gold deposits in the unit squares, equal to either \textbf{0}, \textbf{1} or \textbf{2}. The first row is the northernmost one, and every row is printed from its western end to the eastern one. \OutputFile For each test case, output: \begin{itemize} \item either a single word \textbf{IMPOSSIBLE}, if there is no viable proposition of the land division, \item or the description of the border, if a suitable proposition exists. Print a sequence of at most \textbf{2n} letters \textbf{N}, \textbf{E}, \textbf{S} or \textbf{W}, describing the border from the northwest to the southeast corner. The letters mean that the border goes north, east, south or west, respectively. \end{itemize} If there are multiple solutions, output any one of them.
Time limit 1 second
Memory limit 128 MiB
Input example #1
1
5
2 0 1 0 2
0 0 2 0 0
0 0 2 1 0
1 1 0 0 0
0 2 0 0 0
Output example #1
SEESEESESS
Source 2013 Petrozavodsk Winter Training Camp, Jagiellonian University Contest, January 25, Problem D