#K636. Collision Detection of Moving Creatures
Collision Detection of Moving Creatures
Collision Detection of Moving Creatures
In this problem, you are given n creatures. Each creature starts at an initial point and moves linearly at a constant speed towards its destination. The movement of each creature can be represented by the parametric equation:
\( P(t) = P_{start} + t \times (P_{end} - P_{start}) \) for \(t \in [0,1]\)
Your task is to determine if there exists any pair of creatures that meet (i.e. have the exact same position at the same time \(t\)) at some moment before reaching their destinations. If any two creatures clash, output CLASH
; otherwise, output SAFE
.
Collision Condition Details
For any two creatures, with start points \(A\) and \(B\) and destination points \(A'\) and \(B'\) respectively, they collide if there exists a time \(t\) (with \(0 \le t \le 1\)) such that:
\( A + t \times (A' - A) = B + t \times (B' - B) \)
This condition implies that for each coordinate \(k\) (x, y, and z):
\( A_k + t \times (A'_k - A_k) = B_k + t \times (B'_k - B_k) \)
For each coordinate, if \(A'_k - A_k - (B'_k - B_k) = 0\) then we require that \(A_k = B_k\). Otherwise, the equation gives a candidate time \( t = \frac{B_k - A_k}{(A'_k - A_k) - (B'_k - B_k)} \). The collision occurs if the calculated values of \(t\) are the same across the coordinates (taking into account potential floating point precision) and \(t\) is within the interval \([0,1]\). Note that if all such differences are zero and the starting positions are the same, the creatures are colliding for all \(t\).
inputFormat
The input is read from standard input (stdin) and has the following format:
- The first line contains an integer n (the number of creatures).
- Then follow n lines, each containing 6 integers. The first three integers represent the starting coordinates \(x, y, z\) of a creature, and the next three integers represent its destination coordinates \(x, y, z\).
outputFormat
Output to standard output (stdout) should be a single line containing the string "CLASH" if any two creatures meet at the same time, or "SAFE" if none of them do.
## sample3
0 0 0 10 0 0
0 0 0 5 5 0
0 0 0 0 10 10
CLASH