Problem Statement
At time 0, a rat enters the tube via the entrance. The rat can move inside the tube in both directions (clockwise and counterclockwise). The rat can change direction in an instant. Its maximum speed is Vrat.
At time T, the cat will enter the tube via the same entrance. The cat can also move in both directions and change its direction instantly. Its maximum speed is Vcat.
For the purpose of this problem, you should imagine the tube as a circle with radius R, and the rat and the cat as points on said circle.
The cat is trying to catch the rat as quickly as possible. The rat is trying not to be caught, and if that is impossible, to be caught as late as possible. At any time (starting at time 0) the cat and the rat know each other's position. Already at time 0 the rat knows the time T. Assume that both the cat and the rat behave optimally.
You are given the
Definition
- Class:
- CatAndRat
- Method:
- getTime
- Parameters:
- int, int, int, int
- Returns:
- double
- Method signature:
- double getTime(int R, int T, int Vrat, int Vcat)
- (be sure your method is public)
Notes
- The cat and rat cannot leave the tube.
- Your return value must have a relative or absolute error less than 1e-9.
Constraints
- R, T, Vrat, and Vcat will each be between 1 and 1000, inclusive.
Examples
10
1
1
1
Returns: -1.0
The rat can survive. During the first unit of time it has to run away from the entrance. Then, once the cat enters the tube, the rat can always run in the same direction and with the same speed as the cat.
10
1
1
2
Returns: 1.0
The cat is now faster than the rat. The best strategy for the rat is to enter the tube and to start running in either direction at its maximum speed. The cat will then enter the tube, run in the same direction as the rat, and catch it in exactly 1 unit of time.
10
1
2
1
Returns: -1.0
The rat is now faster than the cat. It can survive, for example by using the strategy described in Example 0.
1000
1000
1
1000
Returns: 1.001001001001001
1
1000
1
2
Returns: 3.141592653589793
1000
1000
999
1000
Returns: 3141.592653589793
1000
1000
1
1000
Returns: 1.001001001001001
1
1000
1
1000
Returns: 0.0031447373909807737
1
1
1
1000
Returns: 0.001001001001001001
1000
1
1
1000
Returns: 0.001001001001001001
1
1
1000
999
Returns: -1.0
1
1
1000
1000
Returns: -1.0
1000
1000
3
4
Returns: 3000.0
1000
1000
4
5
Returns: 3141.592653589793
1000
1000
1000
1000
Returns: -1.0
1000
1000
1000
1
Returns: -1.0
109
24
345
435
Returns: 3.804817769347638
234
54
23
45
Returns: 33.415121860909615
100
1
314
315
Returns: 314.0
100
1
315
316
Returns: 314.1592653589793
1
1
1
1
Returns: -1.0
10
50
1
2
Returns: 31.41592653589793
2
2
4
5
Returns: 6.283185307179586
1
1000
34
989
Returns: 0.00328962581527727
122
598
2
100
Returns: 3.9109622830403548
3
3
4
5
Returns: 9.42477796076938
1
1
998
1000
Returns: 1.5707963267948966
10
7
10
20
Returns: 3.141592653589793
3
3
3
4
Returns: 9.0
1
5
1
2
Returns: 3.141592653589793
1000
1
1
2
Returns: 1.0
1
2
1
1000
Returns: 0.002002002002002002
1
1000
5
7
Returns: 1.5707963267948966
6
6
5
6
Returns: 18.84955592153876
1
1000
2
3
Returns: 3.141592653589793
10
1
40
50
Returns: 3.141592653589793
10
100
3
5
Returns: 15.707963267948966
100
314
1
2
Returns: 314.0
10
16
2
3
Returns: 31.41592653589793
3
17
1
2
Returns: 9.42477796076938
10
35
1
2
Returns: 31.41592653589793
100
23
2
3
Returns: 46.0
50
10
6
10
Returns: 15.0
10
17
2
100
Returns: 0.320570678937734
5
10
3
4
Returns: 15.707963267948966