#K55932. Treasure Hunt in the Grid

    ID: 30085 Type: Default 1000ms 256MiB

Treasure Hunt in the Grid

Treasure Hunt in the Grid

You are given a two-dimensional grid of size \(n \times m\). The grid cells are represented by zeros and used only to determine the grid boundaries. An adventurer starts at the top-left corner at position \((0,0)\). You are provided with a string of commands consisting of the characters 'U', 'D', 'L', and 'R', which represent moves up, down, left, and right respectively.

The task is to simulate these moves. If at any point the adventurer moves out of the grid boundaries, the simulation terminates and the answer is \(-1 -1\). Otherwise, after processing all commands, output the final position as two space-separated integers representing the row and column respectively.

Note: The bounds of the grid are defined based on \(n\) rows and \(m\) columns. The initial position is \((0,0)\), i.e. the first row and first column.

inputFormat

The input is read from standard input (stdin) and is structured as follows:

  • The first line contains two integers \(n\) and \(m\) indicating the number of rows and columns of the grid.
  • The next \(n\) lines each contain \(m\) integers. These integers (which will be 0) represent the grid cells.
  • The last line contains a string of commands composed of the characters 'U', 'D', 'L', and 'R'.

For example, an input may look like:

3 4
0 0 0 0
0 0 0 0
0 0 0 0
DDRRU

outputFormat

The output should be written to standard output (stdout) and consist of two integers separated by a space. These integers represent the final row and column positions of the adventurer after processing all commands. If at any point a command causes the adventurer to move out of bounds, output -1 -1.

For example, for the input above, the correct output is:

1 2
## sample
3 4
0 0 0 0
0 0 0 0
0 0 0 0
DDRRU
1 2

</p>