Hole in One
Time limit5sMemory limit256 MB
Find the most walls a ball shot from the origin can destroy by bouncing off axis-aligned walls before dropping into the hole.
- Level
Hard8 of 10
- Topics
- Backtracking, Geometry, Simulation
- Solved
- No attempts yet
Problem
Janine went to her local game store and bought Hole in One, a new mini golf game for her computer. As the name says, the goal is to shoot the ball into the hole with a single shot. The game also borrows from brick breaker games: the playing field holds several walls, and a wall is destroyed when the ball hits it. The score of a successful shot depends on how many walls were destroyed, so Janine wants to know the largest number of walls she can hit while still making a hole in one.
Think of the playing field as the cartesian plane with the ball starting at the origin. Each wall is a line segment parallel to the axis or to the axis, and the walls do not intersect. The diameter of the ball is negligible, so the ball is a single point.

Figure: an illustration of the first sample input. The ball bounces off two walls, at point 1 and at point 2. By the time it passes point 3, that wall has already vanished.
Two things happen whenever the ball hits a wall.
- The direction of the ball changes in the usual way: the angle of incidence equals the angle of reflection.
- The wall that the ball touched is destroyed. Following common video game logic, no rubble remains and the place where the wall stood is completely empty.
A wall segment contains both of its endpoints. If the ball touches a wall exactly at an endpoint, that wall is destroyed too and the ball is reflected by the same rule.
Janine also picks the power of the shot. The power decides how far the ball rolls, and the ball may roll over the hole several times and drop in on whichever pass she wants. So the best shot may roll over the hole first, hit more walls, and only later drop into the hole.
Input
The first line contains one integer (), the number of walls.
The second line contains two integers and , the coordinates of the hole.
Each of the next lines contains four integers , , , and , describing a wall with endpoints and . Exactly one of and holds.
The hole is not at the origin and not on a wall. No two walls touch or intersect. No wall lies completely on the axis or on the axis. All coordinates in the input are integers with absolute value at most .
Output
If there is no way to shoot the ball so that it reaches the hole, print impossible. Otherwise print the largest number of walls that can be destroyed in a single hole in one shot.