#C9065. Final Position on Grid
Final Position on Grid
Final Position on Grid
You are given a grid of size \(m \times n\) and an initial position \((start_i, start_j)\) on that grid. The rows are numbered from 1 to \(m\) and the columns are numbered from 1 to \(n\).
You will be given a series of movement commands, each in the format DIRECTION k
, where DIRECTION
can be UP
, DOWN
, LEFT
, or RIGHT
and k
is an integer representing the number of steps to move. The commands are executed in order until a command STOP
is encountered. The object cannot move outside the grid boundaries. If a movement command would take the object outside the grid, it stops at the boundary.
Your task is to determine the final position of the object after executing the commands.
inputFormat
The input is read from standard input and consists of multiple lines:
- The first line contains two integers \(m\) and \(n\) (the number of rows and columns).
- The second line contains two integers \(start_i\) and \(start_j\) (the starting row and column).
- The following lines each contain a command. Each command is either of the form
DIRECTION k
(with a space between DIRECTION and k) or the commandSTOP
which indicates the end of commands.
You can assume that the commands will always include a STOP
command to signal the end.
outputFormat
Output the final position of the object as two space-separated integers representing the final row and column respectively, printed to standard output.
## sample6 5
3 3
UP 1
LEFT 2
DOWN 3
RIGHT 1
STOP
5 2