#K66797. Final Position in Matrix
Final Position in Matrix
Final Position in Matrix
You are given an m x n grid (matrix) in which every cell has a value of 0. You start at the top-left corner of the grid (position (0,0)) and you are given a sequence of movement directions. The directions can be one of the following: up, down, left, or right. Your task is to determine your final position after performing all the movements.
Note that if a movement would take you out of the grid boundaries, you must ignore that move. Formally, if the grid has dimensions \( m \times n \), then a move is valid only if the new position \( (x+dx, y+dy) \) satisfies \(0 \le x+dx < m\) and \(0 \le y+dy < n\).
inputFormat
The input is given from stdin with the following format:
- Two integers, m and n, representing the number of rows and columns of the grid.
- m lines follow, each containing n integers (each will be 0) representing the grid.
- An integer d on a new line representing the number of movement directions.
- A single line containing d space-separated strings. Each string is one of "up", "down", "left", or "right".
outputFormat
Output to stdout a single line containing two integers separated by a space: the final row index and the final column index in the grid.
## sample2 2
0 0
0 0
0
0 0
</p>