Collision Detection

Time limit1sMemory limit128 MB

Summary
Given two recent position/time/speed readings per car, decide whether the cars pass within 18 ft at any moment in the next 30 seconds.
Level

Medium7 of 10

Topics
Math, Geometry, Simulation, Implementation
Solved
No attempts yet

Problem

As a preliminary step in building an autonomous-vehicle system, your team must show that a central traffic controller can raise an alert whenever two cars are likely to collide unless someone takes corrective action.

The test course is a set of straight tracks that cross one another at various angles. As a car passes a sensor mounted on a track, its position and speed are recorded and sent to the controller, which keeps the two most recent readings for each car.

The process carries some built-in uncertainty: the sensor readings are not exact, and the sensors cannot tell whether a driver is already aware of the other traffic. The controller can almost never prove that a collision is unavoidable, and even if it could, it would rarely do so in time for the drivers to react.

We therefore want the controller to raise an alert whenever two cars will pass dangerously close to each other at any moment during the next 30 seconds, assuming both keep behaving as they were most recently observed to behave. Two cars are dangerously close if they pass within 18 ft of each other, and safe if their closest approach is at least 20 ft. A closest approach between 18 ft and 20 ft is ambiguous and may be reported either way.

Assume that:

  • each car stays on its straight track;
  • the acceleration (change in speed per unit time) of each car stays constant over the interval between its two readings and for the next 30 seconds, except for the two cases listed below. Acceleration may be negative, meaning the car is slowing down.

If a car with initial speed s0s_0 has constant acceleration aa, then after a time interval tt its speed is

st=a t+s0s_t = a\,t + s_0

and over that same interval it travels a distance

d=a2 t2+s0 t.d = \frac{a}{2}\,t^2 + s_0\,t.

The two exceptions to the constant-acceleration rule are:

  1. a decelerating car stops decelerating once its speed reaches 00 (cars never go into reverse);
  2. an accelerating car stops accelerating once its speed reaches 8080 feet per second (about 55 mph).

Input

The input contains one or more data sets.

Each data set has 4 observations, one per line. The first two observations belong to car 1 and the last two belong to car 2. Each observation is four floating-point numbers tt, xx, yy, ss:

  • tt — the time of the observation in seconds, 0≤t≤1200 \le t \le 120;
  • xx, yy — the car's position in feet, −5000≤x,y≤5000-5000 \le x, y \le 5000;
  • ss — the speed in feet per second, 0≤s≤800 \le s \le 80.

No data set has a closest approach that falls in the ambiguous 18–20 ft range. For each car the two observations happen at distinct times, and the earlier observation is listed first.

The input ends with one observation made of 4 negative numbers, which is not part of any data set.

Output

For each data set, print one line — either Dangerous or Safe — according to whether a dangerously close passage is predicted within the 30 seconds following the largest of the 4 observation times.

Examples3

  1. Example 1

    Input
    10 0 0 10
    11 7.42 7.42 11
    11 41.0 106.0 16
    12 56 106 14
    0 0 0 50
    0.5 21.7 12.5 50.1
    0.25 39.0 22.5 50
    0.75 60.7 35.0 50.1
    -1 -1 -1 -1
    
    Expected output
    Dangerous
    Safe
    
  2. Example 2

    Input
    0 0 0 10
    1 10 0 10
    0 100 0 10
    1 90 0 10
    -1 -1 -1 -1
    
    Expected output
    Dangerous
    
  3. Example 3

    Input
    0 0 0 10
    1 10 0 10
    0 0 50 10
    1 10 50 10
    -1 -1 -1 -1
    
    Expected output
    Safe