#K33122. Find Point on Route

    ID: 25018 Type: Default 1000ms 256MiB

Find Point on Route

Find Point on Route

You are given a starting point A with coordinates \( (x_a, y_a) \) and a series of segments that form a route. Each segment is specified by a direction (one of N, S, E, or W) and a length. The route is constructed by consecutively following these segments. Your task is to determine whether a target point B with coordinates \( (x_b, y_b) \) lies exactly on this route.

If point B lies exactly on one of the segments, output the total distance traveled from point A to reach B along the route. Otherwise, output \(-1\).

For example, suppose you start at \( (0,0) \) and follow the segments: N 3, E 4, S 2. Then:

  • For target point \( (3,-1) \): B does not lie on the route, so output is \(-1\).
  • For target point \( (0,2) \): B lies on the first segment and the distance is \(2\).

Note: Directions have the following meaning: N (increase in \(y\)), S (decrease in \(y\)), E (increase in \(x\)), and W (decrease in \(x\)).

inputFormat

The input is read from standard input (stdin) and has the following format:

  1. The first line contains two space-separated integers \( x_a \) and \( y_a \), representing the coordinates of the starting point A.
  2. The second line contains a single integer \( n \), the number of segments.
  3. The next \( n \) lines each contain a character and an integer: the direction (one of N, S, E, W) and the length of that segment.
  4. The last line contains two space-separated integers \( x_b \) and \( y_b \), representing the coordinates of the target point B.

outputFormat

Output a single integer to standard output (stdout): the distance from A to B along the route if B lies on the route, or \(-1\) if B does not lie on the route.

## sample
0 0
3
N 3
E 4
S 2
3 -1
-1