#P7569. Safe Shield Tunneling
Safe Shield Tunneling
Safe Shield Tunneling
You are given a $1 \times 1$ shield tunneling machine and an $n \times m$ grid. Your task is to plan a route so that the machine passes through every cell exactly once (i.e. performing $n \times m - 1$ moves since the initial placement is not counted as a move).
However, there is a twist: due to a programming fault, if the machine moves continuously in the same direction for exactly $k$ steps, it will get stuck and start dropping TNT repetitively. In other words, no contiguous segment of moves in the same direction should have length exactly $k$.
Find a route that visits every cell exactly once while ensuring the machine is never forced to move continuously in one direction for exactly $k$ moves. Output the sequence of moves as a string consisting of characters 'R' (right), 'D' (down), 'L' (left) and 'U' (up). If no such route exists, output -1.
inputFormat
The input consists of a single line containing three integers:
- n: the number of rows in the grid.
- m: the number of columns in the grid.
- k: the dangerous consecutive move count that must be avoided.
It is guaranteed that $n, m \ge 1$ and $k \ge 1$.
outputFormat
If there exists a valid route, output a string of length $n \times m - 1$ representing the sequence of moves. Otherwise, output -1.
sample
2 3 2
DRURD