poj 3304

Segments
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 13327 Accepted: 4259

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 (x1y1) and (x2y2) 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

题解
这道题实际上是一道很水的计算几何题,共同点可以转化为直线的垂线可以与每条线段相交,直线经过各种旋转平移之后又可以经过至少两个线段的端点,所以直接枚举就行
具体见代码
#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;
const int maxn=107;
struct point{
	double x,y;
	point(double x=0,double y=0):x(x),y(y){
	}
};
typedef point vector;
vector operator + (vector a,vector b){
	return vector(a.x+b.x,a.y+b.y);
}
vector operator - (point a,point b){
	return vector(a.x-b.x,a.y-b.y);
}
vector operator * (vector a,double t){
	return vector(a.x*t,a.y*t);
}
double cross(vector a,vector b){
	return a.x*b.y-a.y*b.x;
}
const double eps=1e-10;
int dcmp(double t){
	if(fabs(t)<eps) return 0;
	else return t<0?-1:1;
}
double dot(vector a,vector b){
	return a.x*b.x+a.y*b.y;
}
bool dpl(point a,point b,point c,point d){
	double c1=cross(b-a,c-a),c2=cross(b-a,d-a);
	return dcmp(c1)*dcmp(c2)<=0;
}
int n;
point a1[maxn][3];
bool judge(point a,point b){
	if(!dcmp(a.x-b.x)&&!dcmp(a.y-b.y)) return 0;
	for(int i=1;i<=n;i++){
		if(!dpl(a,b,a1[i][1],a1[i][2])) return false;
	}
	return true;
}
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
		{
			scanf("%lf%lf%lf%lf",&a1[i][1].x,&a1[i][1].y,&a1[i][2].x,&a1[i][2].y);
		}
		int ok=0;
		for(int i=1;i<=n;i++){
			if(!ok) for(int k=1;k<=n;k++){
				if(judge(a1[i][1],a1[k][1])||judge(a1[i][2],a1[k][1])||judge(a1[i][1],a1[k][2])||judge(a1[i][2],a1[k][2])){
					ok=1;
					break;
				}
			}
		}
		if(ok) printf("Yes!\n");
		else printf("No!\n");
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值