hdu-1147(跨立实验)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1147

思路:判断每条线段,如果将要输入的线段和已经有的线段相交,则这条线段不算。

参考文章:https://blog.csdn.net/hexianhao/article/details/51254487

判断两条线段是否相交:https://www.cnblogs.com/wuwangchuxin0924/p/6218494.html

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
struct Node{
    double x1,y1,x2,y2;
};
struct N{
    int x,y;
};
int pd(Node t1,Node t2)
{
    N a,b,c,d;  
    a.x=t1.x1;a.y=t1.y1;
    b.x=t1.x2;b.y=t1.y2;
    c.x=t2.x1;c.y=t2.y1;
    d.x=t2.x2;d.y=t2.y2;
    double u=(c.x-a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y);
    double v=(d.x-a.x)*(b.y-a.y)-(b.x-a.x)*(d.y-a.y);
    if(u*v>0) return 0;
    double w=(a.x-c.x)*(d.y-c.y)-(d.x-c.x)*(a.y-c.y);
    double z=(b.x-c.x)*(d.y-c.y)-(d.x-c.x)*(b.y-c.y);
    if(w*z>0) return 0;
    return 1;
}
const int maxn = 100100;
Node vc[maxn];
int vis[maxn];
int main(void)
{
    int n,i,j;
    while(scanf("%d",&n)&&n)
    {
        memset(vis,0,sizeof(vis));
        for(i=1;i<=n;i++)
        {
            scanf("%lf%lf%lf%lf",&vc[i].x1,&vc[i].y1,&vc[i].x2,&vc[i].y2);
        }
        int num=0;
        for(i=1;i<n;i++)
        {
            for(j=i+1;j<=n;j++)
            {
                if(pd(vc[i],vc[j]))
                {
                    vis[i]=1;num++;break;
                }
            }
        }
        num=n-num;
        printf("Top sticks:");
        for(j=0,i=1;i<=n;i++)
        {
            if(vis[i]==0)
            {
                j++;
                if(j==num) printf(" %d.\n",i);
                else printf(" %d,",i);
            }
        }
    }
    return 0;
}

 

posted @ 2018-11-06 13:27  麟阁  阅读(230)  评论(0编辑  收藏  举报