poj3304 Segments

Segments
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9898 Accepted: 3050

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!

题意:给出N条线段,问是否存在每条线段的投影交于一点的情况。

也就是说有一条直线经过所有的线段。

题解:要经过这N条线段,通过平移,满足条件,这条直线肯定经过这些线段的至少某两个端点。所以枚举这2*N个端点,看是否这一条直线与这N条线段相交。判断直线与线段相交,可以把线段的两个端点跟直线的位置进行叉积。分别在直线两边(叉积为负)或者某个端点在直线上(叉积为零)则线段与直线相交。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <vector>
#include <ctime>
#define LL __int64
#define eps 1e-8
using namespace std;
struct point
{
	double x,y;
}l[200],r[200],p[210];
double across(point a,point b,point c)
{
	return (a.x-c.x)*(b.y-c.y)-(a.y-c.y)*(b.x-c.x);
}
int go(point a,point b,point c,point d)
{
	double ans=across(a,b,c)*across(a,b,d);
	if (ans<0 || fabs(ans)<eps) return 1;
	else return 0;
}
int main()
{
	int T,i,j,k,n;
	scanf("%d",&T);
	while (T--){
		scanf("%d",&n);
		int cnt=0;
		for (i=1;i<=n;i++){
			scanf("%lf%lf%lf%lf",&l[i].x,&l[i].y,&r[i].x,&r[i].y);
			p[cnt].x=l[i].x;p[cnt++].y=l[i].y;
			p[cnt].x=r[i].x;p[cnt++].y=r[i].y;
		}
		int flag=1;
		for (i=0;i<cnt && flag;i++){
			for (j=i+1;j<cnt && flag;j++){
				if (p[i].x==p[j].x && p[i].y==p[j].y) continue;
				int ff=1;
				for (k=1;k<=n;k++){
					if (!go(p[i],p[j],l[k],r[k])){
						ff=0;
						break;
					}
				}
				if (ff) flag=0;
			}
		}
		if (flag) cout<<"No!"<<endl;
		else cout<<"Yes!"<<endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值