zoukankan      html  css  js  c++  java
  • poj 2653 (线段相交判断)

    http://poj.org/problem?id=2653

    Pick-up sticks
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 9531   Accepted: 3517

    Description

    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.
     
     
    。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
    -------------------------------------------------------------------------
    注意当判断出当前线已经被挡住后,要跳出循环,否者会超时
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <iostream>
    #include <stdlib.h>
    #include <math.h>
    #include <algorithm>
    
    using namespace std;
    #define MAXX 100010
    #define eps 1e-6
    
    typedef struct point
    {
        double x,y;
    }point;
    
    typedef struct
    {
        point st,ed;
    }line;
    
    bool  xy(double x,double y){ return x<y-eps; }
    bool  dy(double x,double y){ return x>y+eps; }
    bool xyd(double x,double y){ return x<y+eps; }
    bool dyd(double x,double y){ return x>y-eps; }
    bool  dd(double x,double y){ return fabs(x-y)<eps; }
    
    double crossProduct(point a,point b,point c)
    {
        return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x);
    }
    
    bool onSegment(point a,point b,point c)
    {
        double maxx=max(a.x,b.x);
        double maxy=max(a.y,b.y);
        double minx=min(a.x,b.x);
        double miny=min(a.y,b.y);
        if(dd(crossProduct(a,b,c),0.0)&&dyd(c.x,minx)
               &&xyd(c.x,maxx)&&dyd(c.y,miny)&&xyd(c.y,maxy))
            return true;
        return false;
    }
    
    bool segIntersect(point p1,point p2,point p3,point p4)
    {
        double d1=crossProduct(p3,p4,p1);
        double d2=crossProduct(p3,p4,p2);
        double d3=crossProduct(p1,p2,p3);
        double d4=crossProduct(p1,p2,p4);
        if(xy(d1*d2,0.0)&&xy(d3*d4,0.0))
            return true;
        if(dd(d1,0.0)&&onSegment(p3,p4,p1))
            return true;
        if(dd(d2,0.0)&&onSegment(p3,p4,p2))
            return true;
        if(dd(d3,0.0)&&onSegment(p1,p2,p3))
            return true;
        if(dd(d4,0.0)&&onSegment(p1,p2,p4))
            return true;
        return false;
    }
    
    point p[MAXX];
    line li[MAXX];
    int num[MAXX];
    
    int main()
    {
        int m,n,i,j;
        while(scanf("%d",&n)!=EOF&&n)
        {
            memset(num,0,sizeof(num));
            for(i=0;i<n;i++)
            {
                scanf("%lf%lf%lf%lf",&li[i].st.x,&li[i].st.y,&li[i].ed.x,&li[i].ed.y);
            }
            for(i=0;i<n;i++)
            {
                for(j=i+1;j<n;j++)
                {
                    if(segIntersect(li[i].st,li[i].ed,li[j].st,li[j].ed))
                    {
                        num[i]++;
                        break;
                    }
    
                }
            }
            int cas=0,tes=0,tmp;
            //int str[MAXX];
            for(i=0;i<n;i++)
            {
                if(num[i] == 0)
                {
                    cas++;
                }
            }
            printf("Top sticks: ");
            for(i=0;i<n;i++)
            {
                if(num[i] == 0 && tes<cas-1)
                {
                    printf("%d, ",i+1);
                    tes++;
                    tmp=i;
                }
            }//printf("%d**",i);
            for(j=tmp+1;j<n;j++)
            {
                if(num[j] == 0)
                {
                    printf("%d.
    ",j+1);
                }
            }
        }
        return 0;
    }
    View Code
  • 相关阅读:
    tensorflow 2.0 学习 (十) 拟合与过拟合问题
    tensorflow 2.0 学习 (九) tensorboard可视化功能认识
    tensorflow 2.0 学习 (八) keras模块的认识
    tensorflow 2.0 学习 (七) 反向传播代码逐步实现
    tensorflow 2.0 学习 (六) Himmelblua函数求极值
    tensorflow 2.0 学习 (五)MPG全连接网络训练与测试
    arp协议简单介绍
    Pthread spinlock自旋锁
    线程和进程状态
    内核态(内核空间)和用户态(用户空间)的区别和联系·
  • 原文地址:https://www.cnblogs.com/ccccnzb/p/3879192.html
Copyright © 2011-2022 走看看