zoukankan      html  css  js  c++  java
  • hdu 1147线段判断相交问题

    pc代码

    View Code
    hdu 1147
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<queue>
    using namespace std;
    const int N=1002;
    const double eps=0.0000000001;//注意设置一个精度
    
    struct node
    {
        double ux,uy;
        double lx,ly;
        int num;
        bool tab;
    };
    queue<node> ss;
    
    double jud1(node a,int x,int y)
    {
        return (a.ux-a.lx)*(y-a.ly)-(a.uy-a.ly)*(x-a.lx);
    }
    bool jud2(node a,int x,int y)
    {
        if(a.ux==x&&a.uy==y)return true;
        if(a.lx==x&&a.ly==y)return true;
        return false;
    }
    bool judtoach(node a,node b)
    {
        double k1=jud1(a,b.lx,b.ly);
        double k2=jud1(a,b.ux,b.uy);
        double k3=jud1(b,a.lx,a.ly);
        double k4=jud1(b,a.ux,a.uy);
        if(k1*k2<-eps&&k3*k4<-eps)return true;
        if(k1<eps&&k1>-eps&&jud2(a,b.lx,b.ly))return true;
        if(k2<eps&&k2>-eps&&jud2(a,b.ux,b.uy))return true;
        if(k3<eps&&k3>-eps&&jud2(b,a.lx,a.ly))return true;
        if(k4<eps&&k4>-eps&&jud2(b,a.ux,a.uy))return true;
        return false;
    }
    
    int main()
    {
        int n;
        node a,b;
        while(scanf("%d",&n),n)
        {
            while(!ss.empty())ss.pop();
            for(int i=0;i<n;i++)
            {
                scanf("%lf%lf%lf%lf",&a.ux,&a.uy,&a.lx,&a.ly);
                a.tab=true;//用tab标记队列队尾,到队尾时及时跳出循环
                a.num=i+1;
                ss.push(a);//用一个队列存储所有没有被覆盖的木棒
                b=ss.front();
                ss.pop();
                while(!b.tab)
                {
                    if(judtoach(b,a))//如果这个新加入的木棒与之前一个木棒有交点,把原来的那个木棒去掉
                    {
                        b=ss.front();
                        ss.pop();
                        continue;
                    }
                    ss.push(b);
                    b=ss.front();
                    ss.pop();
                }
                b.tab=false;
                ss.push(b);
            }
            cout<<"Top sticks:";
            while(1)
            {
                a=ss.front();
                cout<<" "<<a.num;
                ss.pop();
                if(ss.empty()){cout<<"."<<endl;break;}
                else cout<<",";
            }
        }
        return 0;
    }

    代码

    View Code
     1 #include <iostream>
     2 #include <stdio.h>
     3 
     4 using namespace std;
     5 
     6 typedef struct node
     7 {
     8     double x;
     9     double y;
    10 }point;
    11 typedef struct ed
    12 {
    13     point a;
    14     point b;
    15     int is_top;
    16 }edge;
    17 edge e[100005];
    18 
    19 point make_e(point x,point y)
    20 {
    21     point a;
    22     a.x = x.x - y.x;
    23     a.y = x.y - y.y;
    24     return a;
    25 }
    26 
    27 double cha(point x,point y)
    28 {
    29     return x.x*y.y - y.x*x.y;
    30 }
    31 double dian(point x,point y)
    32 {
    33     return x.x*y.x + y.y*x.y;
    34 }
    35 double cross(edge a,point c)
    36 {
    37     point e1,e2;
    38     e1 = make_e(a.a,c);
    39     e2 = make_e(a.a,a.b);
    40     return cha(e2,e1);
    41 }
    42 
    43 int main()
    44 {
    45     int n,i,j;
    46     while(scanf("%d",&n)&&n)
    47     {
    48         for(i = 0;i < n;i++)
    49         scanf("%lf %lf %lf %lf",&e[i].a.x,&e[i].a.y,&e[i].b.x,&e[i].b.y),e[i].is_top = 0;
    50         int count = n;
    51         for(i = 0;i < n;i++)
    52         {
    53             for(j = i+1;j < n;j++)
    54             {
    55                 if(cross(e[i],e[j].a)*cross(e[i],e[j].b) < 0 && cross(e[j],e[i].a)*cross(e[j],e[i].b) < 0)
    56                 {
    57                     e[i].is_top = 1;
    58                     count--;
    59                     break;
    60                 }
    61 
    62             }
    63         }
    64         printf("Top sticks: ");
    65         count--;
    66         for(i = 0;i < n;i++)
    67         {
    68             if(e[i].is_top == 0){
    69 
    70                 printf("%d",i+1);
    71                 if(count)
    72                 printf(", "),count--;
    73                 else
    74                 printf(".\n");
    75             }
    76         }
    77     }
    78     return 0;
    79 }
  • 相关阅读:
    Java Jsch SFTP 递归下载文件夹
    spring-jms,spring-boot-starter-activemq JmsTemplate 发送方式
    Spring Boot 入门之消息中间件篇(转发)
    Springboot websocket使用
    FinalCutPro快捷键
    基本CSS布局三
    As Simple as One and Two
    Game of Credit Cards
    WOW Factor
    Lose it!
  • 原文地址:https://www.cnblogs.com/0803yijia/p/2640257.html
Copyright © 2011-2022 走看看