您的位置:首页 > 其它

hust 1607--Triangles

2013-03-16 21:00 281 查看
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 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.

Output

For each test case, print a single line contains the total number of triangles in the given figure.
Please see sample for more details

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

给定n个点以及他们的连接情况,问能构成多少三角形=。=做的时候感觉应该可以用类似floyd的方法来判断连接情况,但是每个点的坐标范围
都这么大怎么办呢。此时想到可以用hash的方法来记录,因为每个点的范围到100000,所以只要讲一个点向前移这么多位即可,我这里用了map来
记录

View Code

#include <cstdio>
#include <cstring>
#include <map>
#include <cmath>
#include <algorithm>
using namespace std;
#define eps 1e-8
struct eg
{
double x,y;
}p[220],t,q;
map<double,int>mp;
int link[220][220];
double hash(eg a)
{
return a.x*100000+a.y;
}
double dis(eg a,eg b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int ok(eg a,eg b,eg c)
{
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(fabs(x*t-y*z)<eps) return 1;
return 0;
}
int triange(eg a,eg b,eg c)//判断三角形
{
double x,y,z;
x=dis(a,b);
y=dis(a,c);
z=dis(b,c);
if(x+y-z>eps&&x+z-y>eps&&y+z-x>eps)
return 1;
else return 0;
}
int judge(int i,int j,int k)
{
if(link[i][j]&&link[j][k]&&link[i][k])
return triange(p[i],p[j],p[k]);
return 0;
}
int main()
{
int n,m,i,j,k;
int x,y;
while(scanf("%d%d",&n,&m)!=EOF){
mp.clear();
memset(link,0,sizeof(link));
for(i=0;i<n;i++){
scanf("%lf%lf",&p[i].x,&p[i].y);
mp[hash(p[i])]=i;//点和坐标对应
}
for(i=0;i<m;i++){
scanf("%lf%lf%lf%lf",&q.x,&q.y,&t.x,&t.y);
x=mp[hash(q)],y=mp[hash(t)];
link[y][x]=link[x][y]=1;
}
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(i!=j){
for(k=0;k<n;k++){
if(i!=j&&j!=k&&k!=i){
if(link[i][k]&&link[i][j]&&!link[j][k]
&&ok(p[i],p[j],p[k]))
link[j][k]=link[k][j]=1;
}
}
}
}
}
int cnt=0;
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
for(k=j+1;k<n;k++)
cnt+=judge(i,j,k);
printf("%d\n",cnt);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: