待思考, poj3304 线段投影

Segments
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 15647 Accepted: 4970
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

#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
#include<cmath>

#define eps 1e-8
/**思路:计算几何。这道题要思考到两点:
1:把问题转化为是否存在一条直线与每条线段都有交点。证明:若存在一条直线l和所有线段相交,作一条直线m和l垂直,则m就是题中要求的直线,所有线段投影的一个公共点即为垂足。
2:枚举两两线段的各一个端点,连一条直线,再判断剩下的线段是否都和这条直线有交点。证明:若有l和所有线段相交,则可保持l和所有线段相交,左右平移l到和某一线段交于端点停止(“移不动了”)。然后绕这个交点旋转。也是转到“转不动了”(和另一线段交于其一个端点)为止。这样就找到了一个新的l满足题意,而且经过其中两线段的端点。
ps:要学会这种从结果反过来证的思考方法....
我的判断线段与直线l是否相交的方法:*/
using namespace std;
//二维点类
struct Point
{
    double x,y;
    Point(double a,double b): x(a), y(b){}
    Point(): x(0), y(0){}
};
double Cross(Point &sp, Point &ep, Point &op)
{
    return (sp.x-op.x)*(ep.y-op.y)-(ep.x-op.x)*(sp.y-op.y);
}
bool IsLineIntersectSegment(Point p1,Point p2,Point s,Point e)
{
    if (Cross(p1,p2,s)*Cross(p1,p2,e)>eps) return 0;
    else return 1;
}

Point ps[1000], pe[1000];
int main(){
int testcase; bool flag1,flag2,flag3,flag4;
cin>>testcase;
while (testcase--){
    int n;
    cin>>n;
    for (int i=0; i<n; i++){
        double x0, y0, x1, y1;
        cin>>x0>>y0>>x1>>y1;
        ps[i]=Point(x0, y0);
        pe[i]=Point(x1, y1);
    }
    if (n==1||n==2) {cout<<"Yes!"<<endl; continue;}
    for (int i=0; i<n; i++){
        for (int j=i+1; j<n; j++){
            flag1=flag2=flag3=flag4=true;
            for (int k=0; k<n; k++){
                if (!IsLineIntersectSegment(ps[i],ps[j],ps[k],pe[k]) || (fabs(ps[i].x-ps[j].x) < eps && fabs(ps[i].y-ps[j].y) < eps))
                    flag1=false;

                if (!IsLineIntersectSegment(ps[i],pe[j],ps[k],pe[k]) || (fabs(ps[i].x-pe[j].x) < eps && fabs(ps[i].y-pe[j].y) < eps))
                    flag2=false;

                if (!IsLineIntersectSegment(pe[i],ps[j],ps[k],pe[k]) || (fabs(pe[i].x-ps[j].x) < eps && fabs(pe[i].y-ps[j].y) < eps))
                    flag3=false;

                if (!IsLineIntersectSegment(pe[i],pe[j],ps[k],pe[k]) || (fabs(pe[i].x-pe[j].x) < eps && fabs(pe[i].y-pe[j].y) < eps))
                    flag4=false;

                if (!(flag1 || flag2 || flag3 || flag4))
                    break;
                }

            if (flag1 || flag2 || flag3 || flag4) break;
        }
        if (flag1 || flag2 || flag3 || flag4) break;
    }
    if (flag1 || flag2 || flag3 || flag4) cout<<"Yes!"<<endl;
        else cout<<"No!"<<endl;
}
}
if (!IsLineIntersectSegment(ps[i],ps[j],ps[k],pe[k]) || (fabs(ps[i].x-ps[j].x) < eps && fabs(ps[i].y-ps[j].y) < eps))
                    flag1=false;

                if (!IsLineIntersectSegment(ps[i],pe[j],ps[k],pe[k]) || (fabs(ps[i].x-pe[j].x) < eps && fabs(ps[i].y-pe[j].y) < eps))
                    flag2=false;

                if (!IsLineIntersectSegment(pe[i],ps[j],ps[k],pe[k]) || (fabs(pe[i].x-ps[j].x) < eps && fabs(pe[i].y-ps[j].y) < eps))
                    flag3=false;

                if (!IsLineIntersectSegment(pe[i],pe[j],ps[k],pe[k]) || (fabs(pe[i].x-pe[j].x) < eps && fabs(pe[i].y-pe[j].y) < eps))

奇怪的题目,这几行如果不加eps 就tm WA了。而且 如果真的是精度问题,应该加了仍然不能ac把。。。。。。。。。。。待思考

新思考2017年10月11日00:17:53
可能Is Line Intersect函数可以解决下图问题,待思考
为什么枚举的两个点要计算重合 仍要思考!

应该这种情况过不了把 gg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值