C - Segments

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!

首先有投影的线段转化为有一条直线可以通过所有的线段。

而直线的极限情况必然取在各个直线的端点。所以可以直接遍历所有的端点以及对应直线,可以确定是否有满足题意的直线。

#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<math.h>
#define eps 1e-6
#define LL long long
using namespace std;

const int N = 5000+10;
int sum[N];
int cnt[1005];

struct Point {
	double x, y;
	Point(double x=0, double y=0): x(x), y(y) {}					//构造函数
	Point operator + (Point p) {return Point(x+p.x, y+p.y);}				//两向量相加
	Point operator - (Point p) {return Point(x-p.x, y-p.y);}				//两向量相减
	Point operator * (double k) {return Point(k*x, k*y);}				//向量的数乘
	Point operator / (double k) {return Point(x/k, y/k);}				//向量的数乘(乘以一个分数)
	bool operator < (const Point &p) const {return x!=p.x ? (x<p.x):(y<p.y);}	//给点排序时使用(可以根据需要变化)
	double norm() {return x*x + y*y;}							//向量的范数
	double abs() {return sqrt(norm());}							//向量的大小
	double dot(Point p) {return x*p.x + y*p.y;}					//两向量内积
	double cross(Point p) {return x*p.y - y*p.x;}					//两向量外积
}point[4*N];

typedef Point Vector;

int ccw(Point p0, Point p1, Point p2) {
	Vector v1 = p1-p0;
	Vector v2 = p2-p0;
	if(v1.cross(v2) > 0) return 1;			//逆时针方向
	if(v1.cross(v2) < 0) return -1;			//顺时针方向
	if(v1.cross(v2) == 0) return 0;
	if(v1.dot(v2) < 0) return 2;			//同一直线上的反方向
	if(v1.norm() < v2.norm()) return -2;	//同一直线上的正方向
	return 0;								//在向量上
}

struct Line {
    Point a;
    Point b;
} line[N];



double dist(Line m){
	return sqrt((m.a.x-m.b.x)*(m.a.x-m.b.x)+(m.a.y-m.b.y)*(m.a.y-m.b.y));
}

bool checkline(Line m, int n){
	if(dist(m) == 0) return false;  //一定要考虑重点
    for(int i = 0; i < n; i++)
        if(int f = ccw(line[i].a,m.a,m.b)*ccw(line[i].b,m.a,m.b)>0)
            return false;
    return true;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--) {
    	double x1,y1,x2,y2;
    	int n;
    	scanf("%d",&n);
        for(int i=0,j=0; i<n&&j<2*n; i++,j+=2) {
            scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);
            point[j].x=x1;
            point[j].y=y1;
            point[j+1].x=x2;
            point[j+1].y=y2;
            line[i].a.x=x1;
            line[i].a.y=y1;
            line[i].b.x=x2;
            line[i].b.y=y2;
        }
        bool flag=false;
        for(int i=0; i<2*n; i++)
            for(int j=0; j<2*n ;j++){
				Line v;
				v.a=point[i],v.b=point[j];
				if(checkline(v,n)){
					flag=true;
					break;
				}
        }
        if(flag) printf("Yes!\n");
        else printf("No!\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值