poj3449 多边形的相交问题,输入输出比较麻烦

来源:互联网 发布:win10开机后打不开软件 编辑:程序博客网 时间:2024/06/06 22:17
#include<iostream>#include<stdio.h>#include<string.h>#include<string>#include<stdlib.h>#include<cmath>#include<queue>#include<algorithm>using namespace std;#define rd(x) scanf("%d",&x)#define rdd(x,y) scanf("%d%d",&x,&y)#define rddd(x,y,z) scanf("%d%d%d",&x,&y,&z)#define rdddd(x,y,z,w) scanf("%d%d%d%d",&x,&y,&z,&w)#define rds(s) scanf("%s",s)#define rep(i,n) for(int i=0;i<n;i++)#define LL long longconst int N = 1e5+10;const int M=5e5+10;const int inf=1e9;const double eps=1e-8;const int MOD=1e9+7;int n,m,k;struct Point{    double x,y;    Point(){}    Point(double _x,double _y){        x=_x;y=_y;    }};int sgn(double x) {return x<-eps?-1:x<eps?0:1;}double cross(Point a,Point b,Point c){    return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);}struct node{    char ch;    int n;    Point point[100];    bool operator<(const node&o) const{        return ch<o.ch;    }    void print(){        cout<<ch<<" "<<"n: "<<n<<endl;        for(int i=1;i<=n;i++) cout<<point[i].x<<" "<<point[i].y<<endl;    }}p[30];bool inter(Point a,Point b,Point c,Point d){    if( max(a.x,b.x)>=min(c.x,d.x) &&         max(c.x,d.x)>=min(a.x,b.x) &&         max(a.y,b.y)>=min(c.y,d.y) &&         max(c.y,d.y)>=min(a.y,b.y) &&         sgn(cross(a,b,c))*sgn(cross(a,b,d))<=0 &&         sgn(cross(c,d,a))*sgn(cross(c,d,b))<=0) return true;    return false;}bool jiao(node a,node b){    for(int i=1;i<=a.n;i++){        for(int j=1;j<=b.n;j++){            Point p1,p2,p3,p4;            p1=a.point[i];            if(i==a.n) p2=a.point[1];            else p2=a.point[i+1];            p3=b.point[j];            if(j==b.n) p4=b.point[1];            else p4=b.point[j+1];            if(inter(p1,p2,p3,p4)) return true;        }    }    return false;}int main(){#ifndef ONLINE_JUDGEfreopen("aaa","r",stdin);#endif    int T;    char ch;    char s[20];    while(cin>>ch,ch!='.'){        int i=0;        while(ch!='-'){            i++;            scanf("%s",s);            p[i].ch=ch;            if(s[0]=='s'){                p[i].n=4;                double a,b,c,d;                scanf(" (%lf,%lf) (%lf,%lf)",&a,&b,&c,&d);                p[i].point[1]=Point(a,b);                p[i].point[2]=Point((a+c+b-d)/2.0,(b+d+c-a)/2.0);                p[i].point[3]=Point(c,d);                p[i].point[4]=Point((a+c+d-b)/2.0,(b+d+a-c)/2.0);            }else if(s[0]=='l'){                p[i].n=2;                double u,v,w,z;                scanf(" (%lf,%lf) (%lf,%lf)",&u,&v,&w,&z);                p[i].point[1]=Point(u,v);                p[i].point[2]=Point(w,z);            }else if(s[0]=='t'){                  p[i].n=3;                double u,v,w,z;                scanf(" (%lf,%lf) (%lf,%lf)",&u,&v,&w,&z);                p[i].point[1]=Point(u,v);                p[i].point[2]=Point(w,z);                 scanf(" (%lf,%lf) ",&u,&v);                 p[i].point[3]=Point(u,v);            }else if(s[0]=='p'){                 scanf("%d",&p[i].n);                 for(int j=1;j<=p[i].n;j++){                    double u,v;                    scanf(" (%lf,%lf)",&u,&v);                    p[i].point[j]=Point(u,v);                 }            }else if(s[0]=='r'){                double a,b,c,d,e,f;                scanf(" (%lf,%lf) (%lf,%lf) (%lf,%lf)",&a,&b,&c,&d,&e,&f);                p[i].n=4;                p[i].point[1]=Point(a,b);                p[i].point[2]=Point(c,d);                p[i].point[3]=Point(e,f);                p[i].point[4]=Point(a+e-c,f+b-d);            }            cin>>ch;        }        int cnt=i;        sort(p+1,p+1+cnt);        int ans[100];        int tot=0;        for(int i=1;i<=cnt;i++){             tot=0;              for(int j=1;j<=cnt;j++) if(i!=j){                   if(jiao(p[i],p[j])) ans[tot++]=j;              }             // cout<<tot<<endl;              printf("%c ",p[i].ch);              if(tot==0) puts("has no intersections");              else{                printf("intersects with ");                if(tot==1)  printf("%c\n",p[ans[0]].ch);                else if(tot==2) printf("%c and %c\n",p[ans[0]].ch,p[ans[1]].ch);                else for(int j=0;j<tot;j++){                    if(j==tot-1) printf("and %c\n",p[ans[j]].ch);                    else printf("%c, ",p[ans[j]].ch);                }              }        }        puts("");    }    return 0;}

0 0
原创粉丝点击