Decide whether one cue shot from a fixed line can chain three collisions to sink two balls in opposite corner holes.
Medium7GeometryMathNo attempts yetTime limit1sMemory limit256 MBYou are building a billiards puzzle game. The player places the cue ball at one exact spot, shoots it at one exact angle, and sinks several balls with that single shot.
The collision rule in this game is not real physics. Let ball B be moving and ball A be at rest. At the moment they touch, let n be the unit vector from the center of B to the center of A, and let v be the unit vector of B's direction of travel. Ball A starts moving along n, and ball B keeps moving along v−2(v⋅n)n. Only A's direction matches real physics. Ball B bounces the way it would if A were glued to the table. The speed of a ball does not matter in this problem.

Figure 1
The table is a rectangle of width w and length l. With the bottom left corner at the origin, the left hole is at (0,l) and the right hole is at (w,l). Sinking a ball requires the center of the ball to pass directly over the center of the hole. The table has no cushions, so a ball that leaves the w×l region simply falls off. You do not have to consider collisions with the table itself.

Figure 2
Balls 1, 2 and 3 sit on the table. The player places the cue ball on the dashed line that lies h units above the bottom edge of the table, picks a distance d from the left edge and an angle θ, and shoots. Both of these must happen.
All three collisions must be real. At the moment a moving ball touches a ball at rest, its direction v and the direction n between the two centers satisfy v⋅n>0.
Given w, l, h, the positions of the three balls and the radius r, decide whether this shot is possible. At most one pair (d,θ) satisfies the conditions. In every input where such a shot exists, no two balls touch other than in the three collisions listed above.
The first line contains the width w and the length l of the table (1≤w≤120, 1≤l≤120).
The second line contains eight positive integers r, x1, y1, x2, y2, x3, y3, h. Here r is the radius of every ball including the cue ball (r≤5), (xi,yi) is the center of ball i, and h is the distance from the dashed line to the bottom edge of the table (r≤h≤l/2).
No two balls overlap, although they may touch at a point. All three balls lie between the dashed line and the top edge of the table, and each of them lies completely on the table.
If the shot is possible, print on one line the distance d at which to place the cue ball on the dashed line and the angle θ at which to shoot it, separated by a space. Report θ in degrees, measured counterclockwise from the positive x direction, with 0≤θ<360. Round both values to the nearest hundredth and always print two digits after the decimal point, even when those digits are zero.
The cue ball must also lie completely on the table, so r≤d≤w−r is required. If no d and θ meet every condition, print impossible.