## Light Cyclin'

View as PDF

Points: 20
Time limit: 4.5s
Memory limit: 64M

Author:
Problem type
##### University of Toronto ACM-ICPC Tryouts 2012

Having been sucked into your father's secret computer through a projector in the back of his arcade (or something), you find yourself in the wonderful world of Tron! Here, you play games all day, and if you ever lose, you die.

One such game involves you and an opponent driving around a flat grid on light cycles, which leave behind a permanent trail of...light...wherever they go. This grid can be modeled with the Cartesian plane, and is enclosed by a rectangle of impenetrable walls which ensure that the -coordinate of each light cycle is always between and , while its -coordinate is between and (inclusive). Light cycles always stay on the grid lines, and move at a speed of square per second.

A match lasts seconds. You start at coordinates and follow a set of instructions, with your -th instruction consisting of moving squares in the direction given by the character (with U, D, L, and R representing up, down, left, and right, respectively). Similarly, your opponent starts at coordinates and follows a set of instructions, with their th instruction described by and . Of course, neither player's instructions will ever take them beyond the boundaries of the walls, and it will take each player exactly seconds to execute their instructions. Additionally, for each player, no instruction will have an equal or opposite direction to that of their previous instruction. Finally, if a grid point is ever visited more than once throughout the course of the match, it is guaranteed that one of the path segments intersecting there is passing directly through vertically, while the other is passing directly through horizontally (as such, this cannot happen at either player's starting or ending points).

Whenever both light cycles reach the same grid point at the same time, or a light cycle hits an existing trail of light (in other words, a grid point which either light cycle had previously passed through), a collision occurs. Because you're just playing a practice match for now, neither player dies when this occurs, and, in fact, the collision is not counted in favour of either you or your opponent. Instead, for scenarios as described above, you're simply interested in the number of collisions that will occur throughout each match.

#### Input Specification

Line 1: 1 integer,
For each scenario:
Line 1: 1 integer,
Next line: 3 integers, , , and
Next lines: 1 character, , and 1 integer, , for
Next line: 3 integers, , , and
Next lines: 1 character, , and 1 integer, , for

#### Output Specification

For each scenario:
1 integer: The total number of collisions that will occur.

#### Sample Input

1
12
2 5 5
R 4
U 1
L 1
D 4
L 2
3 3 4
U 3
L 2
D 2
R 5

#### Sample Output

4

#### Explanation of Sample

The following diagram illustrates the paths of the light cycles (yours drawn in solid lines, and your opponent's drawn in dotted ones), as well as all of the collision points (indicated with large dots):