三角形的个数

Problem A. Triangles
Input le: stdin
Output le: stdout
Time limit: 2 seconds

You are given a gure consisting ofnpoints in a 2D-plane and m segments connecting some of them.
We guarantee that any two segments don't share points except their ends and there's no more than one
segment between the same pair of points. Please count the total number of triangles in the given gure.

Input
There're multiple test cases. In each case:
The rst line contains two positive integersnandm. (n200,m20000)
Each of the followingnlines contains two real numbers xi and yi indicating the coordinates of the i-thpoint. ( 100000< xi; yi <100000)
Each of the following m lines contains four real numbers xi, yi, xj, yj. It means (xi; yi) and (xj; yj) are connected by a segment. We guarantee that these points are part of the givennpoints.

Output
For each test case, print a single line contains the total number of triangles in the given gure.

Sample input and output

input

4 5
0 0
1 1
2 0
1 0
0 0 1 1
1 1 2 0
2 0 1 0
1 0 0 0
1 1 1 0

output

3

///////////////////////////////////////////

例子 第一个数4 代表有4个点,5代表5个连线,下面4行是输入4个点的坐标,再下面5行是5组连线  比如:(0,0)与(1,1)相连。本题求有多少个三角形。难点就在于,N个点 共线,不好计算三角形的个数。本题可以用F[n][n] 全部赋初值为0,用数字记录点。如果第I个点与第J个点相连,那么f[i][j]和f[j][i]都附上值为1。

/////////////////////////////////////////////

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#define N 222
#define eps 0.00001
using namespace std;
struct point
{
  double x;
  double y;      
}p[N];
   int n,m;
double f[N][N];
int okk(int q,int w,int e)
{
  return (p[q].y-p[w].y)*(p[w].x-p[e].x)==(p[q].x-p[w].x)*(p[w].y-p[e].y);//判断是否共线
}
int main()
{   
   while(cin>>n>>m)
   {
      memset(f,0,sizeof(f));
   memset(p,0,sizeof(p));
      int i,j,k;
      for(i=1;i<=n;i++)
       {  double e,g;
         cin>>e>>g;
         p[i].x=e;
         p[i].y=g;               
       }  //记录点             
       for(i=1;i<=m;i++)
       {  double a,b,c,d;
        int p1=0,p2=0;
          cin>>a>>b>>c>>d;
          for(j=1;j<=n;j++)
    { 
     if(fabs(p[j].x-a)<=eps&&fabs(p[j].y-b)<=eps) {p1=j;break;}    
           }                
           for(k=1;k<=n;k++)
           {
      if(fabs(p[k].x-c)<=eps&&fabs(p[k].y-d)<=eps) {p2=k;break;}                
           }
           f[p1][p2]=1;
           f[p2][p1]=1;
       }  //记录连线   
       for(i=1;i<=n;i++)//下面是记录共线的,如有共线的 记录到数组里面
        for(j=1;j<=n;j++)
  {
             if(f[i][j])
             {
                for(k=1;k<=n;k++)
                  if((i!=k)&&(j!=k)&&(f[k][i])&&okk(i,j,k)) {f[k][j]=1;f[j][k]=1;}  //代码的最关键的地方 注意只能从i与k有连线,j与i有连线,且三点共线,才能推出j与k相连         
             }             
        }
    int sum=0;
       for(i=1;i<=n;i++)//找三角形 找到了用sum记录
         for(j=i+1;j<=n;j++)
         if(f[i][j])
         {
           for(k=j+1;k<=n;k++)
           {
      if(f[j][k]&&f[i][k]&&(!okk(i,j,k)))   {sum++;}//i与j相连 j与k相连 k与i相连 且i,j,k 三点不共线才能构成三角形                
           }
         }
       cout<<sum<<endl;
   }
   return 0;
}

 

### 关于东方博宜 OJ 1079 三角形个数的解题思路 对于编号为1079的问题——即计算特定条件下能够形成的三角形数量,这类题目通常涉及几何学以及组合数学的知识。具体到此题目的解答上,可以采用枚举的方法来解决。 #### 枚举法解析 假设给定了一组点或者是边的信息,目标是在这些条件之下找出能构成的有效三角形数目。如果直接考虑三个顶点之间的关系,则复杂度较高;因此更高效的方式是从已知数据出发逐步构建可能存在的三角形结构[^1]。 - **输入处理**:首先读取必要的参数,比如平面上不共线的若干整数坐标点集或者其他形式表示的潜在三角形边界信息。 - **逻辑核心**: - 对每一个候选三元组 (A, B, C),验证它们是否满足形成闭合路径的要求,也就是任意两边之和大于第三边的原则。 - 使用循环遍历所有可能性,并通过计数器记录符合条件的情况总数。 - **输出结果**:最后打印出统计得到的合法三角形单位数量作为最终答案。 ```cpp #include <iostream> using namespace std; int main() { // 假设这里有一个函数getTriangleCount接收一些参数并返回三角形的数量 int count = getTriangleCount(/* 参数 */); cout << "The number of triangles is: " << count; } ``` 需要注意的是上述伪代码中的`getTriangleCount()`应被实现成具体的算法流程以适应实际问题场景下的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值