#C1081. Robot Movement on a Grid

    ID: 40056 Type: Default 1000ms 256MiB

Robot Movement on a Grid

Robot Movement on a Grid

You are given a grid of size \(R \times C\). Each cell in the grid is either empty denoted by a dot (.) or contains an obstacle denoted by a hash (#). There are \(K\) robots placed on the grid at given starting positions. Each robot receives a sequence of movements consisting of the characters 'U' (up), 'D' (down), 'L' (left), and 'R' (right).

For each movement, the robot moves one cell in the specified direction if and only if the target cell exists within the grid boundaries and is not an obstacle. Otherwise, the robot remains in its current position.

Your task is to compute and output the final positions of the robots after executing their respective movement sequences.

inputFormat

The input is given via standard input in the following format:

R C
row1
row2
... (R rows in total)
K
x1 y1 movements1
x2 y2 movements2
... (K lines for robots)

Where:

  • \(R\) and \(C\) are the number of rows and columns of the grid.
  • Each of the next \(R\) lines represents a row of the grid.
  • \(K\) is the number of robots.
  • Each robot is described by its starting row \(x\), starting column \(y\) (both 1-indexed), and a string of movements.

outputFormat

For each robot, output a line containing two integers representing the final row and column positions of the robot after performing its moves. The results should be printed in the same order as the input.

## sample
5 5
.....
.....
.....
.....
.....
3
1 1 RRR
3 3 DDD
5 5 LLL
1 4

5 3 5 2

</p>