#P3282. Bubble Fish Shooter

    ID: 16539 Type: Default 1000ms 256MiB

Bubble Fish Shooter

Bubble Fish Shooter

Fish is a fish living in the sea. One day he got bored and decided to play a game called Bubble Fish—a twist on the human game Bubble Shooter with his own approximate rules. We model the underwater region as a plane. The top boundary of this region is at a distance H from the launch point, and the left and right boundaries are at a distance W from the launch point. Initially, several bubbles of various colors (all circles having diameter 2) are fixed at given positions in the region. Two bubbles of the same color are said to be adjacent if they are tangent (i.e. their boundaries touch). They are connected if they are adjacent or can be linked via a sequence of adjacent, same-colored bubbles. Note that initially, even if more than two bubbles are connected, they do not disappear.

Fish plays by shooting bubbles from the launch point (0, 0). For each shot, he is given an angle (in degrees, measured from the positive x‐axis in the counterclockwise direction) and a color. The bubble travels in a straight line. Its movement stops at the first moment when its circle becomes tangent to a wall or an existing bubble. (Here, a collision with a wall occurs when the bubble’s circle touches the boundary. Since each bubble has radius 1, the effective limits are at x = ±(W − 1) and y = H − 1.)

After coming to rest, if the newly placed bubble is connected (via adjacent same‑colored bubbles) with at least one other bubble (i.e. the connected group has more than one bubble), then all bubbles in that connected group disappear. Fish is awarded points equal to the square of the number of bubbles removed. Bubbles removed in one shot are no longer present for future shots. Also, Fish cannot shoot the next bubble until the previous shot stops.

The equations used in the simulation are as follows. Let \( (t \cos \theta, t \sin \theta) \) be the center of the shot bubble at time \(t\), with \(\theta\) given in radians. The bubble collides with an existing bubble at \( (x_i,y_i) \) (which has radius 1) when their distance satisfies:
\[ (t \cos \theta - x_i)^2 + (t \sin \theta - y_i)^2 = 2^2 = 4. \] Similarly, collision with a wall occurs when the center reaches x = \(\pm (W-1)\) or y = H-1 (if moving upward).

Given the initial bubbles and a list of shots, compute the final total score.

inputFormat

Input Format:
The first line contains three numbers: H W N, where H is the distance from the launch point to the top boundary, W is the distance from the launch point to the left/right boundaries, and N is the number of initial bubbles.
The next N lines each contain two real numbers and a string: x y color, representing the center coordinates and color of a bubble. (All bubbles have a diameter of 2.)
Then, an integer M is given, representing the number of shots.
Each of the next M lines contains a real number and a string: angle color, where angle is in degrees (measured from the positive x‑axis in the counterclockwise direction) and color is the color of the shot bubble.

outputFormat

Output the final total score as an integer.

sample

10 5 2
0 9 red
2 9 red
1
90 red
9

</p>