#K76117. Determining Final Coordinates on a Constrained 2D Grid
Determining Final Coordinates on a Constrained 2D Grid
Determining Final Coordinates on a Constrained 2D Grid
You are given a 2D grid navigation problem where you must calculate the final coordinates after processing a series of directional moves. Each move has its own distance, and each direction ('U' for Up, 'D' for Down, 'L' for Left, and 'R' for Right) has a specific maximum number of allowed moves. If at any point the number of moves in any direction exceeds the allowed maximum, the sequence is considered invalid and you should output Invalid
.
Formally, let the final coordinates be \( (x, y) \) such that:
[ x = \text{total distance moved right} - \text{total distance moved left}, \quad y = \text{total distance moved up} - \text{total distance moved down} ]
The constraints are given by:
- Up moves count must not exceed u_max.
- Down moves count must not exceed d_max.
- Left moves count must not exceed l_max.
- Right moves count must not exceed r_max.
If the moves are valid, output the coordinates as two space-separated integers. Otherwise, output Invalid
(without quotes).
inputFormat
The input is read from stdin and consists of multiple lines:
- The first line contains four space-separated integers u_max, d_max, l_max, r_max representing the maximum allowed moves in each direction.
- The second line contains a single integer n indicating the number of moves.
- The next n lines each contain a character (one of 'U', 'D', 'L', 'R') followed by a space and an integer representing the distance of that move.
outputFormat
If the sequence of moves is valid, print the final coordinates as two space-separated integers x y
. If the sequence is invalid due to violating any of the directional limits, print Invalid
.
3 2 2 3
5
U 1
R 2
D 1
R 1
L 1
2 0