给定大小为N * 3的矩阵lines [] [] ,使得第i行表示具有方程式lines [i] [0] * x + lines [i] [1] * y = lines [i] [ [2] ,以及整数X和Y ,分别表示点(X,Y)的坐标,任务是计算给定矩阵中在给定点(X,Y)上彼此相交的线数。
例子:
Input: N=5, X = 3, Y = 4, Lines[][] = {{4, -1, 8}, {2, -7, -2}, {1, 1, 7}, {1, -3, 5}, {1, 0, 3}}
Output: 3
Explanation:
Lines 4*x – y = 8, 1*x + 1* y = 7 and 1*x – 0*y = 3 intersect with each other at point (3, 4).
Input: N=4, X = -2, Y = 3, Lines[][] = {{3, -2, -12}, {1, 3, 5}, {1, -1, -5}, {2, 3, 4}}
Output: 2
Explanation:
Lines 3*x – 2*y = -12 and 1*x – 1*y = -5 intersect with each other at point (-2, 3).
天真的方法:解决问题的最简单方法是,对于每条线,找到其与其他线的交点,并检查它们是否在给定点(X,Y)处相交。最后,打印与(X,Y)相交的行数。
时间复杂度: O(N 2 )
辅助空间: O(1)
高效方法:可以通过观察来优化上述方法:
If two lines pass through the same point, then they will definitely intersect at that point.
请按照以下步骤解决问题:
- 因此,计算通过给定点的线数,令线数为cnt 。
- 计算完上述步骤后,打印相交总数:
Count of intersections of cnt lines intersecting at (X, Y) = cntC2
下面是上述方法的实现:
C++
// C++ Program to implement
// the above approach
#include
using namespace std;
// Structure to store point
struct Point {
int x, y;
};
// Structure to store line
struct Line {
int a, b, c;
};
// Function to check if a line
// passes through a point or not
bool point_lies_on_line(Line l,
Point p)
{
// Condition for the line
// to pass through point p
if (l.a * p.x + l.b * p.y
== l.c) {
return true;
}
return false;
}
// Function to find lines
// intersecting at a given point
int intersecting_at_point(
vector& lines, Point p)
{
int cnt = 0;
for (int i = 0; i < lines.size(); i++) {
// If the point lies on a line
if (point_lies_on_line(lines[i], p)) {
// Increment cnt
cnt++;
}
}
// Count of intersections
int ans = (cnt * (cnt - 1)) / 2;
// Return answer
return ans;
}
// Driver Code
int main()
{
// Number of lines
int N = 5;
// Point (x, y)
Point p = { 3, 4 };
// Array to store the lines
vector lines;
lines.push_back({ 4, -1, 8 });
lines.push_back({ 1, 0, 3 });
lines.push_back({ 1, 1, 7 });
lines.push_back({ 2, -7, -2 });
lines.push_back({ 1, -3, 5 });
// Function call
int ans = intersecting_at_point(lines, p);
cout << ans << endl;
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Structure to store point
static class Point
{
int x, y;
public Point(int x, int y)
{
super();
this.x = x;
this.y = y;
}
};
// Structure to store line
static class Line
{
int a, b, c;
public Line(int a, int b, int c)
{
super();
this.a = a;
this.b = b;
this.c = c;
}
};
// Function to check if a line
// passes through a point or not
static boolean point_lies_on_line(Line l,
Point p)
{
// Condition for the line
// to pass through point p
if (l.a * p.x + l.b * p.y == l.c)
{
return true;
}
return false;
}
// Function to find lines
// intersecting at a given point
static int intersecting_at_point(Vector lines,
Point p)
{
int cnt = 0;
for(int i = 0; i < lines.size(); i++)
{
// If the point lies on a line
if (point_lies_on_line(lines.get(i), p))
{
// Increment cnt
cnt++;
}
}
// Count of intersections
int ans = (cnt * (cnt - 1)) / 2;
// Return answer
return ans;
}
// Driver Code
public static void main(String[] args)
{
// Number of lines
int N = 5;
// Point (x, y)
Point p = new Point(3, 4);
// Array to store the lines
Vector lines = new Vector();
lines.add(new Line(4, -1, 8));
lines.add(new Line(1, 0, 3));
lines.add(new Line(1, 1, 7));
lines.add(new Line(2, -7, -2));
lines.add(new Line(1, -3, 5));
// Function call
int ans = intersecting_at_point(lines, p);
System.out.print(ans + "\n");
}
}
// This code is contributed by Amit Katiyar
Python3
# Python3 program to implement
# the above approach
# Function to check if a line
# passes through a poor not
def point_lies_on_line(l, p):
# Condition for the line
# to pass through pop
if (l[0] * p[0] + l[1] * p[1] == l[2]):
return True
return False
# Function to find lines
# intersecting at a given point
def intersecting_at_point(lines, p):
cnt = 0
for i in range(len(lines)):
# If the polies on a line
if (point_lies_on_line(lines[i], p)):
# Increment cnt
cnt += 1
# Count of intersections
ans = (cnt * (cnt - 1)) // 2
# Return answer
return ans
# Driver Code
if __name__ == '__main__':
# Number of lines
N = 5
# Po(x, y)
p = [ 3, 4 ]
# Array to store the lines
lines = []
lines.append([ 4, -1, 8 ])
lines.append([ 1, 0, 3 ])
lines.append([ 1, 1, 7 ])
lines.append([ 2, -7, -2 ])
lines.append([ 1, -3, 5 ])
# Function call
ans = intersecting_at_point(lines, p)
print(ans)
# This code is contributed by mohit kumar 29
C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG{
// Structure to store point
class Point
{
public int x, y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
};
// Structure to store line
class Line
{
public int a, b, c;
public Line(int a,
int b, int c)
{
this.a = a;
this.b = b;
this.c = c;
}
};
// Function to check if a line
// passes through a point or not
static bool point_lies_on_line(Line l,
Point p)
{
// Condition for the line
// to pass through point p
if (l.a * p.x + l.b * p.y == l.c)
{
return true;
}
return false;
}
// Function to find lines
// intersecting at a given point
static int intersecting_at_point(List lines,
Point p)
{
int cnt = 0;
for(int i = 0; i < lines.Count; i++)
{
// If the point lies on a line
if (point_lies_on_line(lines[i], p))
{
// Increment cnt
cnt++;
}
}
// Count of intersections
int ans = (cnt * (cnt - 1)) / 2;
// Return answer
return ans;
}
// Driver Code
public static void Main(String[] args)
{
// Number of lines
int N = 5;
// Point (x, y)
Point p = new Point(3, 4);
// Array to store the lines
List lines = new List();
lines.Add(new Line(4, -1, 8));
lines.Add(new Line(1, 0, 3));
lines.Add(new Line(1, 1, 7));
lines.Add(new Line(2, -7, -2));
lines.Add(new Line(1, -3, 5));
// Function call
int ans = intersecting_at_point(lines, p);
Console.Write(ans + "\n");
}
}
// This code is contributed by Rajput-Ji
3
时间复杂度: O(N)
辅助空间: O(1)