CCC '02 S5 - Follow the Bouncing Ball

View as PDF

Submit solution

Points: 10
Time limit: 1.0s
Memory limit: 256M

Problem type
Canadian Computing Competition: 2002 Stage 1, Senior #5

Suppose you want to create a screen saver which has a ball bouncing around the edges. The action will continue until the ball hits a corner (like a pool table). At that point, the ball disappears off the screen and the screen goes black.

Balls bounce off the walls according to the laws of physics. The angle of the ball as it approaches the wall (the angle of incidence) will equal the angle of the ball as it bounces away (the angle of reflection).

Write a program which will determine how many bounces it will take before the bouncing ball will be swallowed by a corner, if at all. The screen dimensions are in the range [100, 1000] units. Consider the ball as a point on the screen. The corner pockets are 5 units along each wall, and if a ball hits the wall at the edge of a pocket it will continue bouncing.

NOTE: The ball will always hit the right wall first.

A sample screen is shown below.

Input Specification

Input consists of four integers on separate lines.

  • n - The width of the screen. (100 \le n \le 1000)
  • m - The height of the screen. (100 \le m \le 1000)
  • p - The initial position of the ball at the bottom of the screen. (5 \le p \le n-5)
  • q - The position of the ball when it bounces off the right wall. (5 \le q \le m-5)

Output Specification

The output consists of an integer which indicates the number of bounces the ball makes before it disappears in a corner. Count the hit off the right wall as the first bounce. It is possible that the ball will never bounce into a corner (assuming a frictionless surface), in this case, output should be 0. Note that when the ball hits a pocket is not counted as a bounce. You do not have to program the actual animation.

Sample Input

300
200
200
100

Sample Output

2

Comments