#C4811. Final Position on Grid

    ID: 48391 Type: Default 1000ms 256MiB

Final Position on Grid

Final Position on Grid

In this problem, you are given a grid with obstacles and free cells. The grid is represented by (m) rows and (n) columns where each cell is either free ('.') or blocked ('#'). You are provided with a starting position and a sequence of movement directions (each being one of 'U', 'D', 'L', 'R'). Your task is to simulate the moves under the following rules:

  1. You can only move into a cell if it is within the grid boundaries and is not blocked (i.e., it is not marked as '#').
  2. You must process the moves in order from the given string until you have attempted (\text{max_moves}) moves or you finish the directions.

Determine the final position ((x, y)) of the player after performing the movement instructions. This is a simulation problem that requires careful checking of bounds and obstacles while counting moves.

inputFormat

The input is given via standard input and has the following format:

  • The first line contains two space-separated integers (m) and (n), representing the number of rows and columns of the grid.
  • The next (m) lines each contain a string of length (n) consisting of characters '.' (free cell) and '#' (obstacle).
  • The following line contains two space-separated integers (start_x) and (start_y), representing the starting coordinates (0-indexed).
  • The next line contains a string of characters representing the sequence of directions to move ('U', 'D', 'L', 'R').
  • The last line contains an integer (max_moves), the maximum number of moves to attempt.

outputFormat

Print the final position of the player as two space-separated integers (x) and (y) on a single line.## sample

3 3
...
.#.
...
0 0
RRDD
4
2 2