#C5115. Hamiltonian Tour on Grid

    ID: 48729 Type: Default 1000ms 256MiB

Hamiltonian Tour on Grid

Hamiltonian Tour on Grid

Given an n×nn\times n grid and a starting cell (r,c)(r, c) (1-indexed), determine whether it is possible to perform a Hamiltonian tour — a path that visits every cell exactly once and returns to the starting cell. A valid tour must be represented as a sequence of moves: U (up), D (down), L (left), and R (right).

For this problem, a valid tour exists only for a 1×11\times 1 grid where no moves are needed. For any grid with n>1n > 1, output impossible.

inputFormat

Input is read from stdin as three space-separated integers: rr, cc, and nn. Here, rr and cc represent the starting row and column (with 1-indexing), and nn specifies the size of the grid (i.e. the grid is n×nn\times n).

outputFormat

Output a single string to stdout representing the move sequence if a Hamiltonian tour exists; otherwise, output impossible. For a 1×11\times 1 grid, output an empty string.## sample

1 1 1