📜  点(X,Y)上的碰撞计数

📅  最后修改于: 2021-04-23 18:54:24             🧑  作者: Mango

给定大小为N * 3的矩阵arr [] [] ,使得每行包含3个定义元素的属性,即(x坐标,y坐标,速度)以及两个整数XY ,任务是计算如果给定数组中的每个元素都以各自的速度朝着给定点(X,Y)沿直线直线移动,则在点(X,Y)上可能发生的碰撞对数。
例子:

方法:我们的想法是简单地找到一条其每一点将达到给定的点(X,Y)的时间。如果两个点同时到达原点,则它们肯定会发生碰撞。现在,要计算每个点花费的时间,请执行以下操作:

在为每个点计算时间2之后,只需检查它们中的哪个相同并计算碰撞次数。
步骤如下:

  • 遍历数组arr []
  • 创建一个新的数组Time [],并为每个点计算时间,并将其追加到此数组中并对其进行排序。
  • 遍历Time []并计算同时到达给定点的元素的数量,并使用该计数计算当时的碰撞数量。
  • 最后,返回碰撞总数。

下面是上述方法的实现:

C++14
// C++14 program to implement
// the above approach
#include 
using namespace std;
  
// Function to find the count of
// possible pairs of collisions
int solve(vector> &D, int N, 
                           int X, int Y)
{
      
    // Stores the time at which
    // points reach the origin
    vector T;
  
    // Calculate time for each point
    for(int i = 0; i < N; i++)
    {
        int x = D[i][0];
        int y = D[i][1];
  
        double speed = D[i][2];
  
        double time = ((x * x - X * X) +
                       (y * y - Y * Y)) /
                       (speed * speed);
  
        T.push_back(time);
    }
  
    // Sort the times
    sort(T.begin(), T.end());
  
    int i = 0;
    int total = 0;
  
    // Counting total collisions
    while (i < T.size() - 1)
    {
          
        // Count of elements arriving at
        // a given point at the same time
        int count = 1;
  
        while (i < T.size() - 1 and 
                T[i] == T[i + 1])
        {
            count += 1;
            i += 1;
        }
  
        total += (count * (count - 1)) / 2;
        i += 1;
    }
    return total;
}
  
// Driver Code
int main()
{
    int N = 5;
  
    // Given set of points with speed
    vector> D = { { 5, 12, 1 },
                              { 16, 63, 5 },
                              { -10, 24, 2 },
                              { 7, 24, 2 },
                              { -24, 7, 2 } };
  
    int X = 0, Y = 0;
  
    // Function call
    cout << (solve(D, N, X, Y));
  
    return 0;
}
  
// This code is contributed by mohit kumar 29


Java
// Java program to implement
// the above approach
import java.util.*;
import java.lang.*;
  
class GFG{
  
// Function to find the count of 
// possible pairs of collisions 
static double solve(int[][] D, int N, 
                        int X, int Y) 
{ 
      
    // Stores the time at which 
    // points reach the origin 
    ArrayList T = new ArrayList<>(); 
  
    // Calculate time for each point 
    for(int i = 0; i < N; i++) 
    { 
        int x = D[i][0]; 
        int y = D[i][1]; 
  
        double speed = D[i][2]; 
  
        double time = ((x * x - X * X) + 
                       (y * y - Y * Y)) / 
                       (speed * speed); 
  
        T.add(time); 
    } 
  
    // Sort the times 
    Collections.sort(T); 
  
    int i = 0; 
    int total = 0; 
  
    // Counting total collisions 
    while (i < (T.size() - 1)) 
    { 
          
        // Count of elements arriving at 
        // a given point at the same time 
        int count = 1; 
  
        while ((i < (T.size() - 1)) && 
               (Double.compare(T.get(i),
                               T.get(i + 1)) == 0))
        { 
            count += 1; 
            i += 1; 
        } 
  
        total += (count * (count - 1)) / 2; 
        i += 1; 
          
    } 
    return total; 
} 
  
// Driver Code
public static void main (String[] args)
{
    int arr[] = { 1, 2, 3, 4, 5 }; 
    int N = 5; 
      
    // Given set of points with speed 
    int[][] D = { { 5, 12, 1 }, 
                  { 16, 63, 5 }, 
                  { -10, 24, 2 }, 
                  { 7, 24, 2 }, 
                  { -24, 7, 2 } }; 
      
    int X = 0, Y = 0; 
      
    // Function call 
    System.out.println(solve(D, N, X, Y)); 
}
}
  
// This code is contributed by offbeat


Python3
# Python3 Program to implement
# the above approach
  
# Function to find the count of
# possible pairs of collisions
def solve(D, N, X, Y):
  
    # Stores the time at which 
    # points reach the origin
    T = [] 
          
    # Calculate time for each point
    for i in range(N):
  
        x = D[i][0] 
        y = D[i][1]
  
        speed = D[i][2]
  
        time = ((x * x - X * X) +
                (y * y - Y * Y)) / (speed * speed) 
                  
  
        T.append(time)
          
    # Sort the times
    T.sort()
      
    i = 0
    total = 0
          
  
    # Counting total collisions
    while i


C#
// C# program to implement
// the above approach
using System;
using System.Collections;
  
class GFG{
  
// Function to find the count of 
// possible pairs of collisions 
static double solve(int[, ] D, int N, 
                        int X, int Y) 
{ 
      
    // Stores the time at which 
    // points reach the origin 
    ArrayList T = new ArrayList(); 
  
    // Calculate time for each point 
    for(int i = 0; i < N; i++) 
    { 
        int x = D[i, 0]; 
        int y = D[i, 1]; 
  
        double speed = D[i, 2]; 
  
        double time = ((x * x - X * X) + 
                       (y * y - Y * Y)) / 
                       (speed * speed); 
  
        T.Add(time); 
    } 
  
    // Sort the times 
    T.Sort(); 
  
    int j = 0; 
    int total = 0; 
  
    // Counting total collisions 
    while (j < (T.Count - 1)) 
    { 
          
        // Count of elements arriving at 
        // a given point at the same time 
        int count = 1; 
      
        while ((j < (T.Count - 1)) && 
              (Convert.ToDouble(T[j]).CompareTo(
               Convert.ToDouble(T[j + 1])) == 0))
        { 
            count += 1; 
            j += 1; 
        } 
        total += (count * (count - 1)) / 2; 
        j += 1; 
    } 
    return total; 
} 
  
// Driver Code
public static void Main (String[] args)
{
    int N = 5; 
      
    // Given set of points with speed 
    int [,] D = new int [,] { { 5, 12, 1 }, 
                              { 16, 63, 5 }, 
                              { -10, 24, 2 }, 
                              { 7, 24, 2 }, 
                              { -24, 7, 2 } }; 
      
    int X = 0, Y = 0; 
      
    // Function call 
    Console.WriteLine(solve(D, N, X, Y)); 
}
}
  
// This code is contributed by jana_sayantan


输出:
4.0

时间复杂度: O(N log N)
辅助空间: O(1)