1 条题解

  • 0
    @ 2025-5-25 18:02:19
    #include<iostream>
    #include<cstdio>
    #include<string>
    #include<cstring>
    #include<vector>
    #include<cmath>
    #include<queue>
    #include<stack>
    #include<map>
    #include<set>
    #include<algorithm>
    using namespace std;
    const int maxn=50;
    const double eps=1e-10;
    int N;
    bool vis[maxn][maxn];
    struct Point
    {
        double x,y;
        Point(double x=0,double y=0):x(x),y(y){}
    };
    pair<int,int> ans[maxn];
    struct node
    {
        int num;
        Point p[maxn];
    }polygon[maxn];
    int dcmp(double x)
    {
        if(fabs(x)<eps)return 0;
        return x<0?-1:1;
    }
    Point operator - (Point A,Point B)
    {
        return Point(A.x-B.x,A.y-B.y);
    }
    bool operator == (const Point &a,const Point &b)
    {
        return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;
    }
    double Cross(Point A,Point B)
    {
        return A.x*B.y-A.y*B.x;
    }
    double Dot(Point A,Point B){return A.x*B.x+A.y*B.y;}
    bool isPointOnSegment(Point p,Point a1,Point a2)
    {
        return dcmp(Cross(a1-p,a2-p))==0&&dcmp(Dot(a1-p,a2-p))<0;
    }
    int isPointInPolygon(Point p,node poly,int a,int b)
    {
        int wn=0;
        int n=poly.num;
        for(int i=0;i<n;i++)
        {
            if(p==poly.p[i])return 1;
            if(isPointOnSegment(p,poly.p[i],poly.p[(i+1)%n]))
            {
                return 1;
            }
            int k=dcmp(Cross(poly.p[(i+1)%n]-poly.p[i],p-poly.p[i]));
            int d1=dcmp(poly.p[i].y-p.y);
            int d2=dcmp(poly.p[(i+1)%n].y-p.y);
            if(k>0&&d1<=0&&d2>0)wn++;
            if(k<0&&d2<=0&&d1>0)wn--;
        }
        if(wn!=0)return 1;
        return 0;
    }
    int main()
    {
        int T,cas=1;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d",&N);
            for(int i=0;i<N;i++)
            {
                int num;
                scanf("%d",&polygon[i].num);
                for(int j=0;j<polygon[i].num;j++)
                {
                    scanf("%lf,%lf",&polygon[i].p[j].x,&polygon[i].p[j].y);
                }
            }
            printf("Data Set #%d\n",cas++);
            memset(vis,0,sizeof(vis));
            bool flag=false;
            int cnt=0;
            for(int i=0;i<N;i++)
            {
                for(int j=0;j<polygon[i].num;j++)
                {
                    bool flag1=false;
                    for(int k=0;k<N;k++)
                    {
                        if(k==i||vis[i][k]||vis[k][i])continue;
                        if(isPointInPolygon(polygon[i].p[j],polygon[k],i,k))
                         {
                             vis[i][k]=vis[k][i]=1;
                             ans[cnt++]=make_pair(min(i,k),max(i,k));
                             flag=true;
                             flag1=true;
                         }
                    }
                }
            }
            if(!flag)printf("no collisions\n");
            else
            {
                sort(ans,ans+cnt);
                for(int i=0;i<cnt;i++)
                    printf("%d %d\n",ans[i].first+1,1+ans[i].second);
            }
        }
        return 0;
    }
    
    • 1

    信息

    ID
    2083
    时间
    1000ms
    内存
    256MiB
    难度
    8
    标签
    递交数
    1
    已通过
    1
    上传者