POJ 3304 Segments(简单计算几何)

Segments
Time Limit: 1000MS Memory Limit: 65536K
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!
Source
Amirkabir University of Technology Local Contest 2006

又是一道基础的计算几何题,就是询问是否存在一条直线穿过给定的所有线段,由于 n n n很小,我们直接暴力枚举两个端点表示直线然后再 O ( n ) O(n) On判断就行了(本蒟蒻因为有个 r e t u r n return return没有写调了 40 m i n 40min 40min)。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define eps 1e-8
#define N 105
using namespace std;
struct pot{double x,y;}p[N<<1];
int n,t;
inline int sign(double x){return (x>eps)-(x<-eps);}
inline pot operator-(pot a,pot b){return pot{a.x-b.x,a.y-b.y};}
inline double cross(pot a,pot b){return a.x*b.y-a.y*b.x;}
inline bool ok(pot a,pot b,pot c,pot d){
	if((cross(a-c,b-c)*cross(a-d,b-d))<=0.0000)return true;
	return false;
}
inline bool pd(pot a,pot b){
	for(int i=1;i<n;i+=2)if(ok(a,b,p[i],p[i+1])==0)return false;
	return true;
}
inline bool check(){
	for(int i=1;i<n;++i)
		for(int j=i+1;j<=n;++j){
			if(sign(p[i].x-p[j].x)==0&&sign(p[i].y-p[j].y)==0)continue;
			if(pd(p[i],p[j]))return true;
		}
	return false;
}
int main(){
	scanf("%d",&t);
	while(t--){
		scanf("%d",&n);
		n<<=1;
		for(int i=1;i<=n;++i)scanf("%lf%lf",&p[i].x,&p[i].y);
		if(check())puts("Yes!");
		else puts("No!");
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值