On a busy road, multiple cars are passing by. A simulation is run to see what happens if brakes fail for all cars on the road. The only way for them to be safe is if they don’t collide and pass by each other. The goal is to identify whether any of the given cars would collide or pass by each other safely around a Roundabout. Think of this as a reference point O ( Origin with coordinates (0,0) ), but instead of going around it, cars pass through it. Considering that each car is moving in a straight line towards the originwith individual uniform speed. Cars will continue to travel in that same straight line even after crossing origin. Calculate the number of collisions that will happen in such a scenario. Note : – Calculate collisions only at origin. Ignore the other collisions. Assume that each car continues on its respective path even after the collision without change of direction or speed for an infinite distance.
Output Format:
A single integer Q denoting the number of collisions at origin possible for given set of cars.
Constraints:
1<=T<=100
1<=N<=5000
Input:
5
5 12 1
16 63 5
-10 24 2
7 24 2
-24 7 2
Output:
4
Explanation:
Let the 5 cars be A, B, C, D, and E respectively.
4 Collisions are as follows –
1) A & B.
2) A & C.
3) B & C.
4) D & E.
Solution: In Python 3
import math
C=int(input())
T={}
collision=0
for i in range(C):
x,y,v=list(map(int,input().split()))
t=math.sqrt(((x/v)**2+(y/v)**2))
if(T.get(t)==None):
T[t]=1
else:
T[t]=T[t]+1
for keys in T:
if(T[keys]!=1):
collision=collision+(T[keys]*(T[keys]-1))/2
print(int(collision))
Also Checkout