您的位置:首页 > 其它

USCOJ 1231 Triangles(HUST 校赛题)

2014-07-07 20:54 381 查看
Description

You are given a figure consisting of n points 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 figure.

Input

There’re multiple test cases. In each case:
The first line contains two positive integers n and m. (n ≤ 200, m ≤ 20000)
Each of the following n lines contains two real numbers x i and y i indicating the coordinates of the i-th point. (−100000 < x i ,y i < 100000)
Each of the following m lines contains four real numbers x i , y i , x j , y j . It means (x i ,y i ) and (x j ,y j ) are connected by a segment. We guarantee that these points are part of the given n points.

Output

For each test case, print a single line contains the total number of triangles in the given figure. Sample input and output
Sample 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

Sample Output

3

//题意:给你一些平面上的点,和由这些点所连接成的边。问这些边能组成几个三角形。
//第一要解决的问题是如何判断这些边是否构成三角形,显然可以得到只要三个点不共线且每两个点之间有边即可组成三角形。
//第二要解决的问题是若三个点共线,例如0 0 0 1 和0 1 0 2,虽然可以一眼看出这三个点在同一直线上,但0 0 0 2之间的边并没有被保存,因此需要先预处理。
#include <stdio.h>
#include <math.h>
#define eps 0.000001
struct point//用结构体数组保存这些点的坐标。
{
double x,y;
} p[220];
int main()
{
int m,n;
double a,b,c,d;
while(scanf("%d%d",&n,&m)!=EOF)
{
int edge[201][201]= {0};//用二维数组表示两点之间是否有边连接,若定义在外面的话注意要清零。
int i,j,k,l,count=0;
for(i=1; i<=n; i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
for(i=1; i<=m; i++)
{
scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
for(j=1; j<=n; j++)
{
if(fabs(a-p[j].x)<eps&&fabs(b-p[j].y)<eps)
k=j;
if(fabs(c-p[j].x)<eps&&fabs(d-p[j].y)<eps)
l=j;
}
edge[k][l]=edge[l][k]=1;
}
for(i=1; i<=n; i++)//将潜在的边在二维数组进行表示
{
for(j=1; j<=n; j++)
{
for(k=1; k<=n; k++)
{
if(edge[j][i]==1&&edge[i][k]==1)//这个地方要注意点的顺序,若表示成edge[i][j]==1&&edge[j][k]==1,下面为edge[i][k]=edge[k][i]=1。这样表示的话会出错,我也不知道为啥
if(fabs((p[j].x-p[i].x)*(p[k].y-p[i].y)-(p[j].y-p[i].y)*(p[k].x-p[i].x))<eps)//判断是否共线
edge[j][k]=edge[k][j]=1;
}
}
}
for(i=1; i<j; i++)//判断是否构成三角形
{
for(j=i+1; j<k; j++)
{
for(k=j+1; k<=n; k++)
{
if(edge[i][j]==1&&edge[j][k]==1&&edge[i][k]==1)
if(fabs((p[j].x-p[i].x)*(p[k].y-p[i].y)-(p[j].y-p[i].y)*(p[k].x-p[i].x))>eps)//不共线且两两点之间有边则构成一个三角形。
count++;
}
}
}
printf("%d\n",count);//最后结果
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: