给定一个由{x1, y1, x2, y2, x3, y3}形式的N 个三角形组成的数组triangles[][]和一个由M条形如“X=x”的水平线和垂直线组成的数组cut[]或“Y=y”表示线段的方程。任务是打印每个切割相交的三角形的数量,使三角形的左右部分的面积应大于零。
例子:
Input: N = 2, triangles[][] = {{0, 2, 2, 9, 8, 5}, {5, 0, 6, 3, 7, 0}}, M = 3, cuts[] = {“X=2”, “Y=2”, “Y=9”}
Output: 1 1 0
Explanation:
The cut at X = 2 divides the first triangle having vertices (0, 2), (2, 9) and (8, 5)
The cut at Y = 2 divides the second triangle having vertices (5, 0), (6, 3) and (7, 0)
The cut at Y = 9 divides none of the triangles.
Input: N = 2, triangles[][] = {{0, 2, 2, 9, 8, 5}}, M = 3, cuts[] = {“X=7”, “Y=7”, “X=9”}
Output: 1 1 0
Explanation:
The cut at X = 2 divides the first triangle having vertices (0, 2), (2, 9) and (8, 5)
The cut at Y = 2 divides the second triangle having vertices (5, 0), (6, 3) and (7, 0)
The cut at Y = 9 divides none of the triangles.
方法:下面是线段将三角形分成非零面积的两部分的条件:
- 如果在X 轴上进行切割并且它严格位于三角形的最小和最大X 坐标之间,那么它将以这样一种方式划分三角形,即左侧和右侧部分的面积应大于零。
- 类似地,如果在Y 轴上进行切割并且它严格位于三角形的最小和最大Y 坐标之间,那么它将以这样的方式划分三角形,即左侧和右侧部分的面积应大于零.
请按照以下步骤解决问题:
- 创建一个结构来存储每个三角形的最大和最小X和Y坐标。
- 在[0, M – 1]范围内遍历数组cut[]数组。
- 对于每次切割,将计数器计数初始化为0以存储当前切割的答案并开始从j = 0 到 N – 1遍历三角形 []数组。
- 检查每个三角形, cuts[i]的形式为X=x,即垂直切割, x严格位于第i个三角形的最大和最小X坐标之间,增加计数器计数,否则继续检查每个切割的其他三角形.
- 在计算cut[i]的答案后,打印count 。
- 对每个切割重复上述步骤并打印每行切割的三角形计数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Store the minimum and maximum
// X and Y coordinates
struct Tri {
int MinX, MaxX, MinY, MaxY;
};
// Function to convert string to int
int StringtoInt(string s)
{
stringstream geek(s);
int x;
geek >> x;
return x;
}
// Function to print the number of
// triangles cut by each line segment
int TriangleCuts(
vector > Triangle,
string Cuts[], int N, int M, int COL)
{
// Initialize Structure
Tri Minimized[N];
// Find maximum and minimum X and Y
// coordinates for each triangle
for (int i = 0; i < N; i++) {
int x1 = Triangle[i][0];
int y1 = Triangle[i][1];
int x2 = Triangle[i][2];
int y2 = Triangle[i][3];
int x3 = Triangle[i][4];
int y3 = Triangle[i][5];
// Minimum X
Minimized[i].MinX
= min({ x1, x2, x3 });
// Maximum X
Minimized[i].MaxX
= max({ x1, x2, x3 });
// Minimum Y
Minimized[i].MinY
= min({ y1, y2, y3 });
// Maximum Y
Minimized[i].MaxY
= max({ y1, y2, y3 });
}
// Traverse each cut from 0 to M-1
for (int i = 0; i < M; i++) {
string Cut = Cuts[i];
// Store number of triangles cut
int CutCount = 0;
// Extract value from the line
// segment string
int CutVal = StringtoInt(
Cut.substr(2, Cut.size()));
// If cut is made on X-axis
if (Cut[0] == 'X') {
// Check for each triangle
// if x lies b/w max and
// min X coordinates
for (int j = 0; j < N; j++) {
if ((Minimized[j].MinX)
< (CutVal)
&& (Minimized[j].MaxX)
> (CutVal)) {
CutCount++;
}
}
}
// If cut is made on Y-axis
else if (Cut[0] == 'Y') {
// Check for each triangle
// if y lies b/w max and
// min Y coordinates
for (int j = 0; j < N; j++) {
if ((Minimized[j].MinY)
< (CutVal)
&& (Minimized[j].MaxY)
> (CutVal)) {
CutCount++;
}
}
}
// Print answer for ith cut
cout << CutCount << " ";
}
}
// Driver Code
int main()
{
// Given coordinates of triangles
vector > Triangle
= { { 0, 2, 2, 9, 8, 5 },
{ 5, 0, 6, 3, 7, 0 } };
int N = Triangle.size();
int COL = 6;
// Given cuts of lines
string Cuts[] = { "X=2", "Y=2", "Y=9" };
int M = sizeof(Cuts) / sizeof(Cuts[0]);
// Function Call
TriangleCuts(Triangle, Cuts,
N, M, COL);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Store the minimum and maximum
// X and Y coordinates
static class Tri
{
int MinX, MaxX, MinY, MaxY;
};
// Function to convert String to int
static int StringtoInt(String s)
{
return Integer.valueOf(s);
}
static int min(int a, int b, int c)
{
return Math.min(a, Math.min(b, c));
}
static int max(int a, int b, int c)
{
return Math.max(a, Math.max(b, c));
}
// Function to print the number of
// triangles cut by each line segment
static void TriangleCuts(int[][] Triangle,
String Cuts[], int N, int M, int COL)
{
// Initialize Structure
Tri []Minimized = new Tri[N];
for(int i = 0; i < N; i++)
{
Minimized[i] = new Tri();
Minimized[i].MaxX = 0;
Minimized[i].MaxY = 0;
Minimized[i].MinX = 0;
Minimized[i].MinY = 0;
}
// Find maximum and minimum X and Y
// coordinates for each triangle
for(int i = 0; i < N; i++)
{
int x1 = Triangle[i][0];
int y1 = Triangle[i][1];
int x2 = Triangle[i][2];
int y2 = Triangle[i][3];
int x3 = Triangle[i][4];
int y3 = Triangle[i][5];
// Minimum X
Minimized[i].MinX = min(x1, x2, x3);
// Maximum X
Minimized[i].MaxX = max(x1, x2, x3);
// Minimum Y
Minimized[i].MinY = min(y1, y2, y3);
// Maximum Y
Minimized[i].MaxY = max(y1, y2, y3);
}
// Traverse each cut from 0 to M-1
for(int i = 0; i < M; i++)
{
String Cut = Cuts[i];
// Store number of triangles cut
int CutCount = 0;
// Extract value from the line
// segment String
int CutVal = StringtoInt(
Cut.substring(2, Cut.length()));
// If cut is made on X-axis
if (Cut.charAt(0) == 'X')
{
// Check for each triangle
// if x lies b/w max and
// min X coordinates
for(int j = 0; j < N; j++)
{
if ((Minimized[j].MinX) < (CutVal) &&
(Minimized[j].MaxX) > (CutVal))
{
CutCount++;
}
}
}
// If cut is made on Y-axis
else if (Cut.charAt(0) == 'Y')
{
// Check for each triangle
// if y lies b/w max and
// min Y coordinates
for(int j = 0; j < N; j++)
{
if ((Minimized[j].MinY) < (CutVal) &&
(Minimized[j].MaxY) > (CutVal))
{
CutCount++;
}
}
}
// Print answer for ith cut
System.out.print(CutCount + " ");
}
}
// Driver Code
public static void main(String[] args)
{
// Given coordinates of triangles
int[][] Triangle = { { 0, 2, 2, 9, 8, 5 },
{ 5, 0, 6, 3, 7, 0 } };
int N = Triangle.length;
int COL = 6;
// Given cuts of lines
String Cuts[] = { "X=2", "Y=2", "Y=9" };
int M = Cuts.length;
// Function Call
TriangleCuts(Triangle, Cuts,
N, M, COL);
}
}
// This code is contributed by Princi Singh
C#
// C# program for the above approach
using System;
class GFG{
// Store the minimum and maximum
// X and Y coordinates
public class Tri
{
public int MinX, MaxX, MinY, MaxY;
};
// Function to convert String to int
static int StringtoInt(String s)
{
return Int32.Parse(s);
}
static int min(int a, int b, int c)
{
return Math.Min(a, Math.Min(b, c));
}
static int max(int a, int b, int c)
{
return Math.Max(a, Math.Max(b, c));
}
// Function to print the number of
// triangles cut by each line segment
static void TriangleCuts(int[,] Triangle,
String []Cuts,
int N, int M,
int COL)
{
// Initialize Structure
Tri []Minimized = new Tri[N];
for(int i = 0; i < N; i++)
{
Minimized[i] = new Tri();
Minimized[i].MaxX = 0;
Minimized[i].MaxY = 0;
Minimized[i].MinX = 0;
Minimized[i].MinY = 0;
}
// Find maximum and minimum X and Y
// coordinates for each triangle
for(int i = 0; i < N; i++)
{
int x1 = Triangle[i, 0];
int y1 = Triangle[i, 1];
int x2 = Triangle[i, 2];
int y2 = Triangle[i, 3];
int x3 = Triangle[i, 4];
int y3 = Triangle[i, 5];
// Minimum X
Minimized[i].MinX = min(x1, x2, x3);
// Maximum X
Minimized[i].MaxX = max(x1, x2, x3);
// Minimum Y
Minimized[i].MinY = min(y1, y2, y3);
// Maximum Y
Minimized[i].MaxY = max(y1, y2, y3);
}
// Traverse each cut from 0 to M-1
for(int i = 0; i < M; i++)
{
String Cut = Cuts[i];
// Store number of triangles cut
int CutCount = 0;
// Extract value from the line
// segment String
int CutVal = StringtoInt(
Cut.Substring(2, Cut.Length - 2));
// If cut is made on X-axis
if (Cut[0] == 'X')
{
// Check for each triangle
// if x lies b/w max and
// min X coordinates
for(int j = 0; j < N; j++)
{
if ((Minimized[j].MinX) < (CutVal) &&
(Minimized[j].MaxX) > (CutVal))
{
CutCount++;
}
}
}
// If cut is made on Y-axis
else if (Cut[0] == 'Y')
{
// Check for each triangle
// if y lies b/w max and
// min Y coordinates
for(int j = 0; j < N; j++)
{
if ((Minimized[j].MinY) < (CutVal) &&
(Minimized[j].MaxY) > (CutVal))
{
CutCount++;
}
}
}
// Print answer for ith cut
Console.Write(CutCount + " ");
}
}
// Driver Code
public static void Main(String[] args)
{
// Given coordinates of triangles
int[,] Triangle = { { 0, 2, 2, 9, 8, 5 },
{ 5, 0, 6, 3, 7, 0 } };
int N = Triangle.GetLength(0);
int COL = 6;
// Given cuts of lines
String []Cuts = { "X=2", "Y=2", "Y=9" };
int M = Cuts.Length;
// Function Call
TriangleCuts(Triangle, Cuts,
N, M, COL);
}
}
// This code is contributed by Princi Singh
1 1 0
时间复杂度: O(M*N)
辅助空间: O(M+N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。