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.
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 xi and yi indicating the coordinates of the ith point. (-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 given n points.
For each test case, print a single line contains the total number of triangles in the given figure.
Please see sample for more details
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
3
The 7th(2012) ACM Programming Contest of HUST
Problem Setter: Zhou Zhou
下面的都是抄袭大神的 莫要鄙视 额先保存下 感觉这个题很好 有必要保存下
题意:给一些点的坐标和里面的点构成的一些线段,求这些线段可以构成多少个三角形;
下面是大神的代码
#include<math.h> #include<stdio.h> #include<string.h> #include<map> using namespace std; struct node {double x,y;}; node point[220]; double yy[220]; map <double,int > pp ; double get(double x,double y) //点hash { // x+=100000; // y+=100000; x*=200000; x+=y; return x; } int mp[220][220]; double dis(node a,node b) { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } int judgeline(node a,node b,node c)//判断线段共线 注意垂直的时候斜率为0 要单独判断啊 { double x,y,z,t; x=a.x-c.x; y=a.y-c.y; z=b.x-c.x; t=b.y-c.y; if((t<1e-8&&t>-1e-8)&&(y<1e-8&&y>-1e-8))return 1; else { if((t<1e-8&&t>-1e-8)||(y<1e-8&&y>-1e-8))return 0; t=(x/y)-(z/t); return (t<1e-8&&t>-1e-8); } } int subjudge(node a,node b,node c)//判断三角形 { double x,y,z; x=dis(a,b); y=dis(a,c); z=dis(b,c); if(x+y-z>1e-8&&x+z-y>1e-8&&y+z-x>1e-8) return 1; else return 0; } int judge(int a,int b,int c) //判断3个点的关系是否满足 { if(mp[a][b]&&mp[a][c]&&mp[b][c]) return subjudge(point[a],point[b],point[c]); else return 0; } int main() { int n,m,a,b,i,j,k,sum; double x1,y1,x2,y2; while(scanf("%d%d",&n,&m)!=EOF) { pp.clear(); for(i=0;i<n;i++) { scanf("%lf%lf",&point[i].x,&point[i].y); yy[i]=get(point[i].x,point[i].y); //点hash pp[yy[i]]=i; //点与序号的映射 } memset(mp,0,sizeof(mp)); for(i=0;i<m;i++) //建立邻接矩阵 { scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); a=pp[get(x1,y1)]; b=pp[get(x2,y2)]; mp[a][b]=1; mp[b][a]=1; } for(i=0;i<n;i++) //点疏松 { for(j=0;j<n;j++) { if(i!=j) { for(k=0;k<n;k++) { if(i!=k&&i!=j&&j!=k) { if(!mp[j][k]&&mp[i][j]&&mp[i][k]&&judgeline(point[i],point[j],point[k])) mp[j][k]=1,mp[k][j]=1; } } } } } sum=0; for(i=0;i<n;i++) //三角形枚举并判断 for(j=i+1;j<n;j++) for(k=j+1;k<n;k++) sum+=judge(i,j,k); printf("%d\n",sum); } return 0; }