#C7871. Snake Movement Simulation
Snake Movement Simulation
Snake Movement Simulation
This problem requires you to simulate the movement of a snake on a two-dimensional grid. The snake moves according to a sequence of commands. After each command, its position is updated. If the snake ever moves out of the grid boundaries, the output should be Out of bounds
. Otherwise, output the final position.
The grid is zero-indexed, and positions are described by coordinates \( (x, y) \). The available commands are UP
, DOWN
, LEFT
, and RIGHT
.
Input is provided via standard input and output must be written to standard output.
inputFormat
The input consists of the following lines:
- The first line contains two integers ( x ) and ( y ) representing the starting position of the snake.
- The second line contains two integers ( w ) and ( h ) representing the grid width and height.
- The third line contains an integer ( n ), the number of commands.
- If ( n > 0 ), the fourth line contains ( n ) commands separated by spaces.
outputFormat
If the snake remains within grid boundaries after processing all commands, print its final position as two space-separated integers (i.e. x y
). Otherwise, print Out of bounds
.## sample
2 2
5 5
5
UP UP LEFT DOWN RIGHT
2 1