HDU 1086 You can Solve a Geometry Problem too
来源:互联网 发布:淘宝上哪家店牛仔裤好 编辑:程序博客网 时间:2024/06/04 23:36
You can Solve a Geometry Problem too
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9205 Accepted Submission(s): 4508
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.
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.
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
20.00 0.00 1.00 1.000.00 1.00 1.00 0.0030.00 0.00 1.00 1.000.00 1.00 1.00 0.0000.00 0.00 1.00 0.000
Sample Output
13
Author
lcy
Recommend
We have carefully selected several similar problems for you: 1115 1392 2108 2150 1348
分析:
线段判交,分别判断在端点处相交于不是在端点处相交
代码:
#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>#include <iostream>using namespace std;struct Point //点{ double x, y; Point(double x = 0, double y = 0) : x(x), y(y) {} //构造函数,方便代码编写};struct Segment //线段{ Point p1, p2;} S[110];typedef Point Vector; //Vector 是 Point 的别名//点-点=向量Vector operator - (Point A, Point B){ return Vector(A.x - B.x, A.y - B.y);}const double eps = 1e-10;//三态函数,减少精度问题int dcmp(double x){ if (fabs(x) < eps) return 0; else return x < 0 ? -1 : 1;}//两向量的叉积double Cross(Vector A, Vector B){ return A.x * B.y - A.y * B.x;}//判定线段是否“规范相交”bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2){ double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1), c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1); return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0;}//判断是否在端点处相交bool OnSegment(Point a1, Point a2, Point b1, Point b2){ return dcmp(Cross(a1 - b1, a2 - b1)) == 0 || dcmp(Cross(a1 - b2, a2 - b2)) == 0 || dcmp(Cross(b1 - a1, b2 - a1)) == 0 || dcmp(Cross(b1 - a2, b2 - a2)) == 0; return 0;}int main(){ int n; while (~scanf("%d", &n) && n != 0) { for (int i = 1; i <= n; i++) scanf("%lf%lf%lf%lf", &S[i].p1.x, &S[i].p1.y, &S[i].p2.x, &S[i].p2.y); int cnt = 0; for (int i = 1; i < n; i++) { for (int j = i + 1; j <= n; j++) { if (SegmentProperIntersection(S[i].p1, S[i].p2, S[j].p1, S[j].p2) || OnSegment(S[i].p1, S[i].p2, S[j].p1, S[j].p2)) { cnt++; } } } printf("%d\n", cnt); } return 0;}
0 0
- hdu 1086 You can Solve a Geometry Problem too
- hdu 1086 You can Solve a Geometry Problem too
- hdu 1086 You can Solve a Geometry Problem too
- HDU 1086 You can Solve a Geometry Problem too
- hdu 1086 You can Solve a Geometry Problem too
- hdu 1086 You can Solve a Geometry Problem too
- hdu/hdoj 1086 You can Solve a Geometry Problem too
- hdu-1086You can Solve a Geometry Problem too
- HDU 1086 You can Solve a Geometry Problem too
- HDU 1086:You can Solve a Geometry Problem too
- HDU 1086You can Solve a Geometry Problem too
- hdu 1086 You can Solve a Geometry Problem too
- HDU-1086-You can Solve a Geometry Problem too
- HDU 1086 You can Solve a Geometry Problem too
- HDU 1086 You can Solve a Geometry Problem too
- HDU-1086 You can Solve a Geometry Problem too
- HDU-1086-You can Solve a Geometry Problem too
- HDU 1086 You can Solve a Geometry Problem too
- project euler 36
- 米4刷win10问题解答
- NYOJ 119士兵杀敌(三)【RMQ裸题】
- GCD Extreme 【FZU--1969】
- 大数据时代:生活、工作与思维的大变革(上)
- HDU 1086 You can Solve a Geometry Problem too
- Ubuntu15.04+caffe+cuda+cudnn+matlab
- 双向循环链表的实现
- 王道程序员求职宝典阅读笔记20151204
- 数据库常用操作集合_1
- 一个操作系统的实现(1):分析linux下如何运行一个执行文件
- 星空幻想
- 解压版的Tomcat基本配置和安装
- 《电子或通信领域当前的主流技术及其社会需求调查报告》