Billiard
Time limit8sMemory limit256 MB
Predict which stationary ball a struck billiard ball hits first as it reflects off cushions over a path of length 10000.
- Level
Medium7 of 10
- Topics
- Geometry, Simulation
- Solved
- No attempts yet
Problem
A billiard table has a rectangular playing area with no pockets. A cushion runs along all four sides.
You built an ultra precise billiard robot. You put several balls on the table and the robot strikes one of them. The struck ball stops the moment its total travelled distance reaches .
When the ball touches a cushion, it turns like a reflection in a mirror. When it touches a corner, it goes back along the path it came from.
Every ball has radius . Two balls collide when the distance between their centres is , and the struck ball reflects off a cushion when the distance from its centre to that cushion is . The other balls stay where they started until the struck ball reaches one of them.
Predict which ball the struck ball collides with first.
Input
The input is a sequence of datasets. The number of datasets is less than . Each dataset has the following format.
n
w h r vx vy
x1 y1
...
xn yn
The first line holds the number of balls on the table, (). The next line holds five integers , , , , separated by single spaces. and are the width and the length of the playing area (), and is the radius of the balls (). The robot strikes the ball in the direction of the vector (, ).
The next lines give the positions of the balls. Each line holds two integers separated by a single space, and is the centre of the th ball in the initial state (, ). is the north west corner of the playing area and is the south east corner. In the initial state no ball touches another ball or a cushion.
The robot always strikes the first ball in the list. The given values carry no error.
The end of the input is a line holding a single .
Output
For each dataset, print on its own line the number of the ball that first collides with the ball the robot struck. Balls are numbered from in the order they appear in the input, so the struck ball is ball . If the struck ball collides with no ball before it stops, print -1.
No two balls collide with the struck ball first at the same time. Changing by less than changes neither the ball that is hit first nor the way that collision happens.