Light Cyclin'

View as PDF

Submit solution

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 x-coordinate of each light cycle is always between 1 and 10^{12}, while its y-coordinate is between 1 and 10^6 (inclusive). Light cycles always stay on the grid lines, and move at a speed of 1 square per second.

A match lasts S (1 \le S \le 10^{100}) seconds. You start at coordinates (X_A, Y_A) and follow a set of N_A (1 \le N_A \le 10^5) instructions, with your i-th instruction consisting of moving L_{A_i} squares in the direction given by the character D_{A_i} (with U, D, L, and R representing up, down, left, and right, respectively). Similarly, your opponent starts at coordinates (X_B, Y_B) and follows a set of N_B (1 \le N_B \le 10^5) instructions, with their ith instruction described by L_{B_i} and D_{B_i}. Of course, neither player's instructions will ever take them beyond the boundaries of the walls, and it will take each player exactly S 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 T (1 \le T \le 20) 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, T
For each scenario:
Line 1: 1 integer, S
Next line: 3 integers, X_A, Y_A, and N_A
Next N_A lines: 1 character, D_{A_i}, and 1 integer, L_{A_i}, for i = 1 \dots N_A
Next line: 3 integers, X_B, Y_B, and N_B
Next N_B lines: 1 character, D_{B_i}, and 1 integer, L_{B_i}, for i = 1 \dots N_B

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):


Comments

There are no comments at the moment.