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
Sample Output
View Code
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; }
相关文章推荐
- hust 1607 Triangles 校赛 一个很好的题 hash
- 【计算几何】Triangles HUST 1607
- hust 1607 Triangles(经典好题)
- HUST_ACdream区域赛指导赛之手速赛系列(1)(2)D——数学——Triangles
- USCOJ 1231 Triangles(HUST 校赛题)
- ZOJ 1704 Myacm Triangles
- Get Integer sided triangles for which the area/perimeter ratio is integral
- 【笔记】模电—lesson 37 负反馈放大电路增益的一般表达式 (hust)
- hust月赛A题
- hust 1342 (有上下界的最小流)
- USACO Number Triangles
- UVa 10112 - Myacm Triangles
- UVA 375 - Inscribed Circles and Isosceles Triangles
- hust校赛 1615 Matrix 矩阵和其逆矩阵的乘积中不为0的元素个数
- HUST 1611 The mell hell(简单题)
- HUST 1599 Multiple(数学难题)
- virtual hust 2013.6.20 数论基础题目 A - Skew Binary
- virtual hust 2013.6.21 NEFU 挑战编程----数论 G - Marbles
- Triangles
- hust 1017 Exact cover 最小覆盖问题 dancing links