Problem I
The Ice Puzzle
Languages
en
sv
Olle hates crosses. They remind him too much of getting a Wrong Answer verdict in Programmeringsolympiaden. When he arrived at Vallhamra Icehall and saw that there were $K$ ($2 \leq K \leq 20000$) crosses placed on the ice, he became angrier than a raging bull.
Being the problem solver he is, Olle has come up with a plan to solve this disastrous situation. He intends to transform all crosses into circles. The ice rink can be represented as an $N \times M$ grid, where $K$ squares have a cross, and the remaining $N \cdot M - K$ squares are empty. The rows of the grid are numbered from $0$ to $N-1$ from top to bottom, and the columns are numbered from $0$ to $M-1$ from left to right. When Olle is done, all squares that originally had crosses should be transformed to contain circles instead.
Initially, he is at the square marked with S. This square also contains a cross. In a move, he can start moving in one of the directions up, down, right, or left, without the ability to turn. He will continue in the same direction until he reaches a cross or circle and stops on that square. If there are no crosses or circles in the direction Olle is moving, he will collide with the wall, suffering an injury serious enough to force him to abandon his plan. Each time he reaches a cross, he stops completely and changes it from a cross to a circle. If he ever reaches a circle, he also stops completely and transforms it back into a cross in his blind rage. Since he doesn’t have all day, he wants to find a sequence of moves containing at most $10^5$ moves that transforms all crosses into circles. For extra style points, he also wants to finish at the same square as he started, but this is not necessary in all test case groups.
Input
The first line of the input contains three integers $N, M,$ and $R$ ($2 \le N \cdot M \leq 10^6$, $R \in \{ 0, 1\} $). The values $N$ and $M$ represent the number of rows and columns that make up the grid, and $R$ indicates whether you must return to the starting square or not. If $R = 1$, you must return to the start, and if $R = 0$, you can end the sequence of moves at any square.
The following $N$ lines each contain a string of length $M$, where the $i$-th of these ($i = 0, 1, \dots , N-1$) describes how the $i$-th row of the ice rink looks. Each character is either “.”, “S” or “X”. It is guaranteed that exactly one square contains “S” and that the number of “X” is equal to $K - 1$ (since the starting square is also a cross).
Output
If there exists a sequence of moves that transforms all crosses into circles, first output the length of any such sequence. The length can be at most $10^5$. On the next line, output the sequence. It should consist of the characters v, <, ^, and >. v means that Olle should move downward, ^ upward, < to the left, and > to the right. If $R=1$, Olle must also finish on the same square as he started.
If there is no valid sequence of moves, output $-1$ and nothing else. Note that the existence of a valid sequence may depend on whether $R=0$ or $R=1$.
Points
Your solution will be tested on several test case groups. To get the points for a group, it must pass all the test cases in the group.
Group |
Point value |
Constraints |
$1$ |
$20$ |
$R=0, K \leq 100$ |
$2$ |
$5$ |
$R=0, K \leq 5000$ |
$3$ |
$15$ |
$R=0$ |
$4$ |
$10$ |
$R=1, K \leq 100$ |
$5$ |
$30$ |
$R=1, K \leq 10000$ |
$6$ |
$20$ |
$R=1$ |
Explanation of Sample 1
Initially, Olle is on the cross in the upper-left corner, and the grid looks like this:
X.X ... X.X
After the moves >, v and ^, Olle is located at the top-right cell of the grid, and it looks like this:
X.X ... X.O
After having made the moves < and >, he is still located att at the top-right cell of the grid, and it looks like this:
O.O ... X.O
Finally, he will make the moves <, v, ^ to create a cross-free ice rink, where he also finishes at the starting position (this is not a requirement in this test case, as $R=0$):
O.O ... O.O
Sample Input 1 | Sample Output 1 |
---|---|
3 3 0 S.X ... X.X |
8 >v^<><v^ |
Sample Input 2 | Sample Output 2 |
---|---|
2 2 0 S. .X |
-1 |
Sample Input 3 | Sample Output 3 |
---|---|
2 2 0 SX X. |
3 ><v |
Sample Input 4 | Sample Output 4 |
---|---|
2 2 1 SX X. |
-1 |