zoukankan      html  css  js  c++  java
  • poj2653线段相交判断

    Stan has n sticks of various length. He throws them one at a time on the floor in a random way. After finishing throwing, Stan tries to find the top sticks, that is these sticks such that there is no stick on top of them. Stan has noticed that the last thrown stick is always on top but he wants to know all the sticks that are on top. Stan sticks are very, very thin such that their thickness can be neglected.Input

    Input consists of a number of cases. The data for each case start with 1 <= n <= 100000, the number of sticks for this case. The following n lines contain four numbers each, these numbers are the planar coordinates of the endpoints of one stick. The sticks are listed in the order in which Stan has thrown them. You may assume that there are no more than 1000 top sticks. The input is ended by the case with n=0. This case should not be processed.

    Output

    For each input case, print one line of output listing the top sticks in the format given in the sample. The top sticks should be listed in order in which they were thrown.

    The picture to the right below illustrates the first case from input.

    Sample Input

    5
    1 1 4 2
    2 3 3 1
    1 -2.0 8 4
    1 4 8 2
    3 3 6 -2.0
    3
    0 0 1 1
    1 0 2 1
    2 0 3 1
    0
    

    Sample Output

    Top sticks: 2, 4, 5.
    Top sticks: 1, 2, 3.
    

    Hint

    Huge input,scanf is recommended.
    此题是我最讨厌的一类题,tle到我生活不能自理,看了题解后然后就开始wa,最后把之前的tle代码揪出来改了就ac了。。
    线段覆盖问题,算最后没有被覆盖的线段就行了
    先快速排斥试验,然后跨立试验。
    快速排斥试验:
    max(u.a.x,u.b.x)<min(v.a.x,v.b.y)
    &&max(v.a.x,v.b.x)<min(u.a.x,u.b.x)
           &&max(u.a.y,u.b.y)<min(v.a.y,v.b.y)
           &&max(v.a.y,v.b.y)<min(u.a.y,u.b.y)
    跨立试验:
    double mul(point p,point u,point v)
    {
        return (u.x-v.x)*(p.y-u.y)-(u.y-v.y)*(p.x-u.x);
    }
    mul(u.a,v.a,v.b)*mul(u.b,v.a,v.b)<=0&&mul(v.a,u.a,u.b)*mul(v.b,u.a,u.b)<=0
    刚开始自己想的一个方法(有点慢):
    判断两线段是否相交有两种情况:
    1:一条线的端点在另一条上;
    2:两条线的端点分别在另一条的两侧
    这个不需要快速排斥试验,所以很有可能tle。。。。
        if(mul(u.a,v.a,v.b)*mul(u.b,v.a,v.b)<0&&mul(v.a,u.a,u.b)*mul(v.b,u.a,u.b)<0)return 1;
        if(mul(u.a,v.a,v.b)==0&&(u.a.x-v.a.x)*(u.a.x-v.b.x)<=0&&(u.a.y-v.a.y)*(u.a.y-v.b.y)<=0)return 1;
        if(mul(u.b,v.a,v.b)==0&&(u.b.x-v.a.x)*(u.b.x-v.b.x)<=0&&(u.b.y-v.a.y)*(u.b.y-v.b.y)<=0)return 1;
        if(mul(v.a,u.a,u.b)==0&&(v.a.x-u.a.x)*(v.a.x-u.b.x)<=0&&(v.a.y-u.a.y)*(v.a.y-u.b.y)<=0)return 1;
        if(mul(v.b,u.a,u.b)==0&&(v.b.x-u.a.x)*(v.b.x-u.b.x)<=0&&(v.b.y-u.a.y)*(v.b.y-u.b.y)<=0)return 1;
    #include<map>
    #include<set>
    #include<list>
    #include<cmath>
    #include<queue>
    #include<stack>
    #include<vector>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define pi acos(-1)
    #define ll long long
    #define mod 1000000007
    
    using namespace std;
    
    const double eps=1e-8;
    const int N=100005,maxn=100005,inf=0x3f3f3f3f;
    
    struct point{
        double x,y;
    };
    struct line{
       point a,b;
    }l[N];
    
    bool out[N];//如果线段有交点,先放的就out
    
    double mul(point p,point u,point v)
    {
        return (u.x-v.x)*(p.y-u.y)-(u.y-v.y)*(p.x-u.x);
    }
    bool acoss(line u,line v)
    {
        if(max(u.a.x,u.b.x)<min(v.a.x,v.b.y)
           &&max(v.a.x,v.b.x)<min(u.a.x,u.b.x)
           &&max(u.a.y,u.b.y)<min(v.a.y,v.b.y)
           &&max(v.a.y,v.b.y)<min(u.a.y,u.b.y))return 0;
        if(mul(u.a,v.a,v.b)*mul(u.b,v.a,v.b)<=0&&mul(v.a,u.a,u.b)*mul(v.b,u.a,u.b)<=0)return 1;
       /* if(mul(u.a,v.a,v.b)==0&&(u.a.x-v.a.x)*(u.a.x-v.b.x)<=0&&(u.a.y-v.a.y)*(u.a.y-v.b.y)<=0)return 1;
        if(mul(u.b,v.a,v.b)==0&&(u.b.x-v.a.x)*(u.b.x-v.b.x)<=0&&(u.b.y-v.a.y)*(u.b.y-v.b.y)<=0)return 1;
        if(mul(v.a,u.a,u.b)==0&&(v.a.x-u.a.x)*(v.a.x-u.b.x)<=0&&(v.a.y-u.a.y)*(v.a.y-u.b.y)<=0)return 1;
        if(mul(v.b,u.a,u.b)==0&&(v.b.x-u.a.x)*(v.b.x-u.b.x)<=0&&(v.b.y-u.a.y)*(v.b.y-u.b.y)<=0)return 1;*/
        return 0;
    }
    int main()
    {
        int n;
        while(~scanf("%d",&n),n){
            memset(out,0,sizeof(out));
            for(int i=1;i<=n;i++)
                scanf("%lf%lf%lf%lf",&l[i].a.x,&l[i].a.y,&l[i].b.x,&l[i].b.y);
            for(int i=1;i<=n;i++)
            {
                for(int j=i+1;j<=n;j++)
                {
                    if(acoss(l[i],l[j]))
                    {
                        out[i]=1;
                        break;
                    }
                }
            }
            bool flag=0;
            for(int i=1;i<=n;i++)
            {
                if(!out[i])
                {
                   if(flag==0)
                   {
                       printf("Top sticks: %d",i);
                       flag=1;
                   }
                   else printf(", %d",i);
                }
            }
            printf(".
    ");
        }
        return 0;
    }
  • 相关阅读:
    解析大型.NET ERP系统 权限模块设计与实现
    Enterprise Solution 开源项目资源汇总 Visual Studio Online 源代码托管 企业管理软件开发框架
    解析大型.NET ERP系统 单据编码功能实现
    解析大型.NET ERP系统 单据标准(新增,修改,删除,复制,打印)功能程序设计
    Windows 10 部署Enterprise Solution 5.5
    解析大型.NET ERP系统 设计异常处理模块
    解析大型.NET ERP系统 业务逻辑设计与实现
    解析大型.NET ERP系统 多国语言实现
    Enterprise Solution 管理软件开发框架流程实战
    解析大型.NET ERP系统 数据审计功能
  • 原文地址:https://www.cnblogs.com/acjiumeng/p/6686510.html
Copyright © 2011-2022 走看看