Vortex
Time limit0.5sMemory limit512 MB
On an n x n letter grid, a spiral-like walk must always step to the outermost reachable unvisited cell; find the lexicographically largest and smallest such strings.
- Level
Medium7 of 10
- Topics
- Simulation, Greedy, Implementation, Brute force
- Solved
- No attempts yet
Problem
Albert plays a game called "Vortex" on an two-dimensional grid. For convenience, the cell in row and column of the board is written as .
Each cell of the board contains one uppercase English letter ('A'-'Z'). For example, the left figure below shows a board. Cell contains "C" and cell contains "G".
The right figure shows a board. The outermost cells of this board contain "A" or "B" (16 cells in total). The innermost cell contains "Z". The remaining cells contain "X" or "Y" (8 cells in total).

Formally, the "outermost" to "innermost" cells of an board are defined as follows.
- Cells lying in the first or last row or column are the "outermost" cells. In the and boards below, the cells marked "1" are the outermost cells.
- After removing all outermost cells, an board remains. As above, the cells lying in the first or last row or column of the remaining board are the (second) outer cells. The cells marked "2" in the figure below are these.
- Continuing inward, we can define cells from the outermost to the innermost.

The Vortex game selects cells one at a time according to the following rules, for a total of cells.
-
First, choose one of the corner cells , , , or to start. Then continue according to the rules below until every cell has been selected exactly once.
-
Repeatedly choose one of the cells adjacent (up, down, left, right) to the currently selected cell, according to the following rules:
- Rule 1: A cell selected before cannot be selected again.
- Rule 2: Among all cells that can be chosen without breaking Rule 1, you must choose a cell lying on the outermost layer of the board (if several such cells exist, you may choose any one of them).
A string (of length ) obtained by selecting all cells while following these rules is called a "vortex string".
For example, for the board above, a vortex string can be obtained as follows:
-
Turn 1: starting at :
- By Rule 1, the only cells that can be chosen next are or .
- Both lie on the outermost layer of the board, so either may be chosen. In this example we choose .
-
Turn 2: after choosing :
- By Rule 1, the only cells that can be chosen next are or .
- Of these two, lies farther out than , so by Rule 2 we must choose .
-
Turn 3: after choosing :
- By Rule 1, we must choose .
-
Turns 4-9: after choosing , the rules force us to choose the remaining cells in the order , , , , .
-
The vortex string obtained this way is "BCDFIHGEA".
-
If on Turn 2 we choose instead of , we obtain "BEGHIFDCA".
Starting at on the same board gives different vortex strings:
- Choosing after : "IFDCBEGHA"
- Choosing after : "IHGEBCDFA"
We may also start at or .
Albert wonders what the "maximum" and "minimum" vortex strings obtainable from a given board are.
For the example above, the maximum vortex string is "IHGEBCDFA" and the minimum is "BCDFIHGEA".
The maximum (minimum) vortex string is the lexicographically latest (earliest) string among all vortex strings obtainable in the Vortex game.
Input
The first line of input gives the number of test cases .
The first line of each test case gives the board size .
The next lines each contain a string of length .
Output
For each test case, print the maximum vortex string and the minimum vortex string separated by a space, one per line.
Constraints
- The board contains only uppercase English letters ('A'-'Z')