#C6412. Frog Teleportation
Frog Teleportation
Frog Teleportation
In this problem, a frog is situated on a rectangular grid with width W and height H. The frog starts at a position \((x_s, y_s)\) and follows a sequence of teleportation instructions. Each instruction indicates one of the eight possible directions (N, S, E, W, NE, NW, SE, SW) and a corresponding distance. After each move, if the frog's new position does not satisfy the grid constraints \(0 \le x < W\) and \(0 \le y < H\), the process stops immediately and the output should be "Out of Bounds". Otherwise, after processing all instructions, the final valid position of the frog is output.
Your task is to simulate the movement of the frog according to the instructions provided, and determine its final position or report if it goes out of bounds.
inputFormat
The input consists of multiple test cases. For each test case:
- The first line contains four integers: W H xs ys, representing the grid's width, height, and the frog's starting coordinates.
- The next line contains an integer n denoting the number of teleportation instructions.
- Each of the following n lines contains a direction (one of N, S, E, W, NE, NW, SE, SW) and an integer distance.
The input is terminated by a line "0 0 0 0", which should not be processed.
outputFormat
For each test case, if the frog’s position becomes invalid (i.e. goes out of bounds) at any point during the execution of the instructions, output "Out of Bounds". Otherwise, output the final coordinates formatted as "x y" (separated by a space).
## sample10 10 5 5
3
N 3
E 2
SE 4
100 100 50 50
2
NW 25
SE 10
10 10 0 0
2
E 5
N 5
0 0 0 0
Out of Bounds
35 65
5 5
</p>