给定一个大小为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
Javascript
3
时间复杂度: O(N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。