A robot is on a map that can be viewed as a grid with $N$ rows and $M$ columns. Rows are numbered from $1$ to $N$ from top to bottom while columns are numbered from $1$ to $M$ from left to right. The cell on the $i^{\text{th}}$ row, $j^{\text{th}}$ column is denoted as $(i, j)$. Initially, the robot is located at $(1, 1)$.
To perform movement, it receives a command, which is represented by a string $S$. There are two types of operations:
- Paid Operations (Costs $1$ coin per character appearing in $S$):
U: The robot moves upwards by $1$ unit, that is, traveling from cell $(X, Y)$ to cell $(X-1, Y)$.D: The robot moves downwards by $1$ unit, that is, traveling from cell $(X, Y)$ to cell $(X+1, Y)$.L: The robot moves leftwards by $1$ unit, that is, traveling from cell $(X, Y)$ to cell $(X, Y-1)$.R: The robot moves rightwards by $1$ unit, that is, traveling from cell $(X, Y)$ to cell $(X, Y+1)$.- Free operation (Costs $0$ coins):
(): It repeats the instruction sequence inside the brackets twice. An instruction consists of at least $1$ valid operation or nested instruction.
For example, if $S =$ (RU)(L), the robot executes RURULL. The total cost is $3$ coins. Note that () can be nested. For example, if $S =$ ((D)R), the robot executes DDRDDR, and the total cost is $2$ coins.
However, an earthquake suddenly happens and destroys $K$ rectangular areas of the map! Note that $0 \le K \le 1$. If $K = 0$, nothing happens. Otherwise, if $K = 1$, the broken area is a rectangle with its top-left corner located at $(X_1, Y_1)$ while its bottom-right corner is located at $(X_2, Y_2)$.
More formally, for all pairs of $(i, j)$ such that $X_1 \le i \le X_2$ and $Y_1 \le j \le Y_2$, cell $(i, j)$ becomes broken. It is guaranteed that neither cell $(1, 1)$ nor cell $(N, M)$ is broken.
Your task is to send the robot back home, which is located at $(N, M)$, by constructing $S$. $S$ must satisfy the following rules:
- The length of $S$ must be at most $10^6$. Note that you do not have to minimize the length of $S$.
- $S$ can only contain characters
U,D,L,R,(and). - All brackets in $S$ must be well-formed and balanced.
- At any moment, the robot must not go off the map or locate at a broken cell.
- After the command is executed, the robot is located exactly at $(N, M)$.
It is possible that the robot can never go back home. In that case, report -1.
Partial scoring is available for this problem. The fewer coins $S$ uses, the higher score you get. Refer to "Scoring" section for further details.
Input
The first line contains three integers $N, M$ and $K$.
If $K = 1$, the second line contains four integers $X_1$, $Y_1$, $X_2$ and $Y_2$.
Output
If it is not possible to go back home, output -1.
Otherwise, output the string $S$, denoting the command for the robot.
Subtasks
For all test cases:
- $2 \le N, M \le 10^9$
- $0 \le K \le 1$
| Subtask | Score | Additional Constraints |
|---|---|---|
| $1$ | $8$ |
$N, M \le 100$ $K = 0$ |
| $2$ | $19$ | $N, M \le 100$ |
| $3$ | $56$ | $K = 0$ |
| $4$ | $17$ | No Additional Constraints |
Scoring
For a test case, if $S$ violates any of the rules mentioned above, you'll get zero score.
Otherwise, partial scoring will be given for different subtasks.
For subtasks $1$ and $2$, you will receive full score automatically if $S$ is valid.
For subtasks $3$ and $4$, denote the full score of a subtask and the number of coins used in $S$ to be $R$ and $C$ respectively. Your score is calculated according to the following formula:
$$ \text{score} = \begin{cases} R & \text{if } C \le 32 \\ (1 - \frac{3(C-32)}{100})R & \text{if } 32 < C \le 40 \\ (0.76 - \frac{2(C-40)}{100})R & \text{if } 40 < C \le 46 \\ (0.64 - \frac{1.5(C-46)}{100})R & \text{if } 46 < C \le 58 \\ (0.46 - \frac{0.8(C-58)}{100})R & \text{if } 58 < C \le 100 \\ 0 & \text{otherwise.} \end{cases} $$Sample Test Cases
| Input | Output | |
|---|---|---|
| 3 4 0 | (RD)R | |
| 5 5 1 3 1 5 2 |
((RD)) | |
The total cost of this output is $2$. |
||
| 5 5 1 3 1 5 2 |
((R))((D)) | |
This is another valid output which also uses $2$ coins. |
||
Scoring: Per Subtask
Authored by s22f26
Appeared in 2026 Mini Comp 6 (Constructive Algorithms & Special Tasks)