题意:给你多边形 让你判断每个多边形分别有几个多边形与它相交。
这题的输入输出恶心,另外给出正方形对角点求出另两个点的公式
已知一正方形两对角顶点A、C的坐标分别是(ax, ay)、(cx, xy);
求B、D坐标(bx, by)、(dx, dy);
bx = (cx+ax+cy-ay)/2;
by = (cy+ay+ax-cx)/2;
dx = (cx+ax+ay-cy)/2;
dy = (cy+ay+cx-ax)/2;
再就没什么主意的地方,按照模拟吧。我的代码4768B,应该是现在写过最长的吧,这代码莫名其妙C++ CE了要G++。
#include <iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef double PointType; struct point { PointType x,y; }; struct polygon { int num,ansnum; point p[22]; char ans[30],name; }; int n; polygon data[30]; PointType Direction(point pi,point pj,point pk) //判断向量PiPj在向量PiPk的顺逆时针方向 +顺-逆0共线 { return (pj.x-pi.x)*(pk.y-pi.y)-(pk.x-pi.x)*(pj.y-pi.y); } bool On_Segment(point pi,point pj,point pk) { if(pk.x>=min(pi.x,pj.x)&&pk.x<=max(pi.x,pj.x)&&pk.y>=min(pi.y,pj.y)&&pk.y<=max(pi.y,pj.y)) return 1; return 0; } bool Segment_Intersect(point p1,point p2,point p3,point p4) { PointType d1=Direction(p3,p4,p1),d2=Direction(p3,p4,p2),d3=Direction(p1,p2,p3),d4=Direction(p1,p2,p4); if(((d1>0&&d2<0)||(d1<0&&d2>0))&&((d3>0&&d4<0)||(d3<0&&d4>0))) return 1; if(d1==0&&On_Segment(p3,p4,p1)) return 1; if(d2==0&&On_Segment(p3,p4,p2)) return 1; if(d3==0&&On_Segment(p1,p2,p3)) return 1; if(d4==0&&On_Segment(p1,p2,p4)) return 1; return 0; } void getcoordinate(string a,double &x,double &y) { int b[2]= {0,0}; int f=0; for(int i=0,j=0; i<a.length(); i++) { if(a[i]=='-') f=1; else if(a[i]>='0'&&a[i]<='9') { if(f) b[j]=b[j]*10-(a[i]-'0'); else b[j]=b[j]*10+a[i]-'0'; } else if(a[i]==',') j++,f=0; } x=b[0],y=b[1]; } void getp(int num) { double x,y; string coordinate; for(int i=0; i<num; i++) { cin>>coordinate; getcoordinate(coordinate,x,y); data[n].p[i].x=x,data[n].p[i].y=y; } } void getnum(string s) { int x,y; string coordinate; if(s=="line") data[n].num=2,getp(data[n].num); if(s=="triangle") data[n].num=3,getp(data[n].num); if(s=="square") { double x1[4],y1[4]; data[n].num=4; cin>>coordinate; getcoordinate(coordinate,x1[0],y1[0]); cin>>coordinate; getcoordinate(coordinate,x1[2],y1[2]); x1[1]=(x1[2]+x1[0]+y1[2]-y1[0])/2; y1[1]=(y1[2]+y1[0]+x1[0]-x1[2])/2; x1[3]=(x1[2]+x1[0]-y1[2]+y1[0])/2; y1[3]=(y1[2]+y1[0]-x1[0]+x1[2])/2; for(int i=0; i<4; i++) data[n].p[i].x=x1[i],data[n].p[i].y=y1[i]; } if(s=="rectangle") { double x1[4],y1[4],x2,y2; data[n].num=4; cin>>coordinate; getcoordinate(coordinate,x1[0],y1[0]); cin>>coordinate; getcoordinate(coordinate,x1[1],y1[1]); cin>>coordinate; getcoordinate(coordinate,x1[2],y1[2]); x2=x1[2]-x1[1],y2=y1[2]-y1[1]; x1[3]=x1[0]+x2,y1[3]=y1[0]+y2; for(int i=0; i<4; i++) data[n].p[i].x=x1[i],data[n].p[i].y=y1[i]; } if(s=="polygon") { int t; cin>>t; data[n].num=t,getp(t); } } bool judge(polygon a,polygon b) { for(int i=0; i<a.num; i++) for(int j=0; j<b.num; j++) if(Segment_Intersect(a.p[i],a.p[(i+1)%a.num],b.p[j],b.p[(j+1)%b.num])) return 1; return 0; } void solve() { for(int i=0; i<n; i++) { data[i].ansnum=0; for(int j=0; j<n; j++) if(j!=i&&judge(data[i],data[j])) data[i].ans[data[i].ansnum++]=data[j].name; } } int cmp(polygon a,polygon b) { return a.name<b.name; } void output() { sort(data,data+n,cmp); for(int i=0; i<n; i++) { sort(data[i].ans,data[i].ans+data[i].ansnum); cout<<data[i].name<<" "; if(data[i].ansnum==0) puts("has no intersections"); else { cout<<"intersects with"; if(data[i].ansnum==1) cout<<" "<<data[i].ans[0]<<endl; else if(data[i].ansnum==2) cout<<" "<<data[i].ans[0]<<" and "<<data[i].ans[1]<<endl; else for(int j=0; j<data[i].ansnum; j++) if(j<data[i].ansnum-1) cout<<" "<<data[i].ans[j]<<","; else cout<<" and "<<data[i].ans[j]<<endl; } } cout<<endl; } int main() { char c; string s; int x,y; while(1) { n=0; while(cin>>c) { if(c=='.') return 0; if(c=='-') break; cin>>s; data[n].name=c; getnum(s); n++; } solve(); output(); } return 0; }