POJ 3304 Segments
来源:互联网 发布:诺基亚软件怎么下载 编辑:程序博客网 时间:2024/06/06 04:32
Segments
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 15584 Accepted: 4950
Description
Given n segments in the two dimensional space, write a program, which determines if there exists a line such that after projecting these segments on it, all projected segments have at least one point in common.
Input
Input begins with a number T showing the number of test cases and then, T test cases follow. Each test case begins with a line containing a positive integer n ≤ 100 showing the number of segments. After that, n lines containing four real numbers x1 y1 x2 y2 follow, in which (x1, y1) and (x2, y2) are the coordinates of the two endpoints for one of the segments.
Output
For each test case, your program must output “Yes!”, if a line with desired property exists and must output “No!” otherwise. You must assume that two floating point numbers a and b are equal if |a - b| < 10-8.
Sample Input
3
2
1.0 2.0 3.0 4.0
4.0 5.0 6.0 7.0
3
0.0 0.0 0.0 1.0
0.0 1.0 0.0 2.0
1.0 1.0 2.0 1.0
3
0.0 0.0 0.0 1.0
0.0 2.0 0.0 3.0
1.0 1.0 2.0 1.0
Sample Output
Yes!
Yes!
No!
题意:
给你几条线段,问存不存在这样一条直线,将所有线段投影在这条直线上后,所有的线段的投影至少有一个公共点。
思路:
计算几何入门题。
所有线段的投影至少有一个公共点,即存在一条直线与所有线段相交,枚举任意两点作直线,判断是否与全部直线相交。
线段
几个坑点:
1. 当n<3时显然应该输出Yes!
2. 枚举点的时候两点离得太近(距离小于1e-8)时,可以看做一个点。
代码:
#include<iostream>#include<set>#include<cstdio>#include<cmath>#define maxn 105using namespace std;struct point{ double x,y;}p[2*maxn];struct segment{ double x1,x2,y1,y2;}ss[maxn];int n;double cx(point a,point b){ return (a.x)*(b.y)-(b.x)*(a.y);}bool pd(segment s,int a){ double z1,z2; point c,d; c.x=ss[a].x1-s.x1; c.y=ss[a].y1-s.y1; d.x=s.x2-s.x1; d.y=s.y2-s.y1; z1=cx(c,d); c.x=ss[a].x2-s.x1; c.y=ss[a].y2-s.y1; z2=cx(d,c); if(z1*z2>=0) return 1; return 0;}bool judge(int a,int b){ segment s; s.x1=p[a].x; s.y1=p[a].y; s.x2=p[b].x; s.y2=p[b].y; int i; for(i=0;i<n;i++) { if(!pd(s,i)) return 0; } return 1;}const double inv=0.00000001;bool same(int a,int b){ if(sqrt((p[a].x-p[b].x)*(p[a].x-p[b].x)+(p[a].y-p[b].y)*(p[a].y-p[b].y))<inv) return 1; return 0;}int main(){ int t; scanf("%d",&t); while(t--) { scanf("%d",&n); int i,j; for(i=0;i<n;i++) { double a,b,c,d; scanf("%lf%lf%lf%lf",&a,&b,&c,&d); point A,B; A.x=a;A.y=b; B.x=c;B.y=d; p[2*i]=A; p[2*i+1]=B; segment s; s.x1=a;s.y1=b;s.x2=c;s.y2=d; ss[i]=s; } bool flag=0; if(n<=2) flag=1; for(i=0;i<2*n&&!flag;i++) { for(j=0;j<2*n;j++) { if(same(i,j)) continue; if(judge(i,j)) { flag=1; break; } } } if(flag) printf("Yes!\n"); else printf("No!\n"); } return 0;}
- POJ 3304 Segments
- poj 3304 Segments
- poj 3304 Segments
- poj 3304 Segments
- POJ 3304 Segments
- POJ 3304 Segments
- POJ 3304 Segments
- poj 3304 Segments
- poj 3304 Segments
- POJ 3304 Segments
- poj 3304 Segments
- POJ 3304 Segments
- poj 3304 ( segments)
- poj-3304-Segments
- POJ 3304 Segments
- POJ 3304 Segments
- poj 3304 Segments
- POJ 3304 Segments
- jQuery DOM操作筛选
- shell 问题两则
- Sudoku Solver
- 欢迎使用CSDN-markdown编辑器
- 关系数据库
- POJ 3304 Segments
- leetcode 315. Count of Smaller Numbers After Self 这个AC的做法实在是难以想到
- 构建ubuntu根文件系统
- 分布式设计 —— 容错与主备
- 动归---------塔
- Java-递归-查询某一目录下文件名
- Android ADB超简单的安装方法
- 【关于alsa buffer】ALSA编程细节分析
- 如何创建一个简单网页