博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU1086判断线段相交
阅读量:7070 次
发布时间:2019-06-28

本文共 3055 字,大约阅读时间需要 10 分钟。

 

HDU1086

You can Solve a Geometry Problem too

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 10924    Accepted Submission(s): 5393

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
 

 

Author
lcy
 
  可以把线段看成直线,计算出交点,看交点是不是在可行的范围内;注意一下斜率等于0的情况就行了;
 
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 #define ll long long15 #define rep(i,a,b) for(register int i=a;i<=b;i++)16 #define inf 1<<2917 #define re register18 using namespace std;19 #define eps 1e-620 const int N=110;21 struct point{22 double x,y;23 }d[2];24 struct line{25 double k,b;26 double l,r,ly,ry;27 }L[N];28 int n;29 inline bool pd(double x,double y,int k) {30 return (x>=L[k].l&&x<=L[k].r&&y>=L[k].ly&&y<=L[k].ry);31 }32 inline bool judge(int i,int j) {33 if(fabs(L[i].k-L[j].k)<=eps) return 0;34 if(fabs(L[i].k-0)<=eps&&fabs(L[j].k-0)<=eps) return 0;35 if(fabs(L[i].k-0)>eps&&fabs(L[j].k-0)>eps) {36 double x=(L[j].b-L[i].b)/(L[i].k-L[j].k),37 y=L[i].k*x+L[i].b;38 if(pd(x,y,i)&&pd(x,y,j)) return 1;39 else return 0;40 }41 if(fabs(L[i].k-0)<=eps) {42 double x=L[i].l;43 double y=L[j].k*x+L[j].b;44 return (y>=L[j].ly&&y<=L[j].ry);45 }46 double x=L[j].l;47 double y=L[i].k*x+L[i].b;48 return (y>=L[i].ly&&y<=L[i].ry);49 }50 int main() {51 freopen("Y.in","r",stdin);52 freopen("Y.out","w",stdout);53 while(scanf("%d",&n)&&n) {54 for(int i=1;i<=n;i++) {55 scanf("%lf%lf%lf%lf",&d[0].x,&d[0].y,&d[1].x,&d[1].y);56 L[i].l=min(d[0].x,d[1].x),L[i].r=max(d[0].x,d[1].x);57 L[i].ly=min(d[0].y,d[1].y),L[i].ry=max(d[0].y,d[1].y);58 if(fabs(d[1].x-d[0].x)<=eps)59 L[i].k=0;60 else {61 L[i].k=(d[1].y-d[0].y)/(d[1].x-d[0].x);62 L[i].b=d[0].y-L[i].k*d[0].x;63 }64 }65 int ans=0;66 for(int i=1;i

 

转载于:https://www.cnblogs.com/ypz999/p/7107127.html

你可能感兴趣的文章
深受程序员鄙视的外行语录!
查看>>
使用runtime Associate方法关联的对象,需要在主对象dealloc的时候释放么?
查看>>
不健康的IT狗,送给你们一句话
查看>>
进程列表中多个JAVA进程的区分识别
查看>>
IPHONE实景导航开发总结
查看>>
Git常用操作命令
查看>>
正则表达式-断言
查看>>
用git合并分支时,如何保持某些文件不被合并
查看>>
局部代码块、构造代码块、静态代码块
查看>>
聚类分析 ---- K-Means算法
查看>>
C語言最新標準-C11 「轉」
查看>>
SaltStack数据系统-Grains详解
查看>>
课程第三天内容《基础交换 三 》
查看>>
Spring(八):缓存
查看>>
全局函数指针作为模板参数
查看>>
URL access forbidden for unknown reason svn: acces
查看>>
kafka基本命令启动和测试
查看>>
你真的已经搞懂JavaScript了吗?
查看>>
Xmanger4远程桌面Ubuntu 12.04
查看>>
WBS分解
查看>>