hdu 1086
2015-06-08 22:32
417 查看
You can Solve a Geometry Problem too
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8586 Accepted Submission(s): 4177
Problem Description
Many geometry(几何)problems were designed in the ACM/ICPC. And now, I also prepare a geometry problem for this final exam. According to the experience of many ACMers, geometry problems are always much trouble, but this problem is very easy, after all we are now
attending an exam, not a contest :)
Give you N (1<=N<=100) segments(线段), please output the number of all intersections(交点). You should count repeatedly if M (M>2) segments intersect at the same point.
Note:
You can assume that two segments would not intersect at more than one point.
Input
Input contains multiple test cases. Each test case contains a integer N (1=N<=100) in a line first, and then N lines follow. Each line describes one segment with four float values x1, y1, x2, y2 which are coordinates of the segment’s ending.
A test case starting with 0 terminates the input and this test case is not to be processed.
Output
For each case, print the number of intersections, and one line one case.
Sample Input
2 0.00 0.00 1.00 1.00 0.00 1.00 1.00 0.00 3 0.00 0.00 1.00 1.00 0.00 1.00 1.00 0.000 0.00 0.00 1.00 0.00 0
Sample Output
1 3
求多条线段共有几个交点 不计算重复的
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<algorithm> #include<string> #include<map> #define ll __int64 #define minn 1e-8 using namespace std; struct Point { double x,y; }P[10010]; struct Line { Point a,b; }L[110]; ///------------alg 2------------ //叉积 double mult(Point a, Point b, Point c) { return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y); } //aa, bb为一条线段两端点 cc, dd为另一条线段的两端点 相交返回true, 不相交返回false bool intersect(Point aa, Point bb, Point cc, Point dd) { if ( max(aa.x, bb.x)<min(cc.x, dd.x) ) { return false; } if ( max(aa.y, bb.y)<min(cc.y, dd.y) ) { return false; } if ( max(cc.x, dd.x)<min(aa.x, bb.x) ) { return false; } if ( max(cc.y, dd.y)<min(aa.y, bb.y) ) { return false; } if ( mult(cc, bb, aa)*mult(bb, dd, aa)<0 ) { return false; } if ( mult(aa, dd, cc)*mult(dd, bb, cc)<0 ) { return false; } return true; } ///------------alg 2------------ double cross(double x1, double y1, double x2, double y2) { return x1 * y2 - x2 * y1; } void intersec(Point a, Point b, Point c, Point d, double &x, double &y) { double u = cross(d.x - a.x, d.y - a.y, b.x - a.x, b.y - a.y); double v = cross(c.x - a.x, c.y - a.y, b.x - a.x, b.y - a.y); double w = u - v; x = (u * 1.0 * c.x - v * 1.0 * d.x) / (w * 1.0); y = (u * 1.0 * c.y - v * 1.0 * d.y) / (w * 1.0); } int main() { int n; while(~scanf("%d",&n)) { if(n==0) break; int len=0; for(int i=0;i<n;i++) { scanf("%lf%lf%lf%lf",&L[i].a.x,&L[i].a.y,&L[i].b.x,&L[i].b.y); for(int j=0;j<i;j++) { if(intersect(L[i].a,L[i].b,L[j].a,L[j].b)) { double st,ed; bool flag=true ; intersec(L[i].a,L[i].b,L[j].a,L[j].b,st,ed); for(int k=0;k<len;k++) { if(abs(P[k].x-st)<minn&&abs(P[k].y-ed)<minn) { flag=false ; break; } } if(flag) P[len].x=st,P[len].y=ed,len++; } } } printf("%d\n",len); } }
相关文章推荐
- FolderSync :The various features and how to use them
- 2015美赛心得总结
- gcc编译报错:程序中有游离的‘\357’‘\273’‘\277’等
- Extjs显示图片
- Android性能优化第二篇--布局优化
- [转载] TCP协议缺陷不完全记录
- 基于spring jdbc的通用存取层
- vector容器中元素初始化(容器的元素都是副本)
- 第一节:mybatis入门
- HTML第二十节(CSS制作照片墙效果与瀑布流)
- android DialogFragment 回调到 Fragment
- 第二题
- 也谈谈程序员职业规划的几个问题:我的一些故事
- Snackbar新版Toast 从源码角度完全解析
- 15第十四周项目二——用文件保存的学生名单
- schedule vs scheduleAtFixedRate
- 算法-链表实现队列
- 返回一个二维整数数组中最大子数组的和5
- ClangFormat代码格式化
- 邮箱mail 发送类 ASP.NET C#