题意:平面上有一个包含n个点的一笔画,图案是封闭的。线段可以相交,但不会部分重叠,如图,求平面被分成多少部分。
#include<iostream> #include<cstdio> #include<algorithm> #define eps 1e-8 using namespace std; int sig(double a) { return (a>eps)-(a<-eps); } typedef struct point { double x,y; point(double xx=0,double yy=0):x(xx),y(yy){} }vector; bool operator < (point a,point b) { return a.x<b.x || (a.x==b.x && a.y<b.y); } bool operator != (point a,point b) { return a.x!=b.x || a.y!=b.y; } vector operator - (point a,point b) { return vector(a.x-b.x,a.y-b.y); } point operator + (point a,vector b) { return point(a.x+b.x,a.y+b.y); } vector operator * (point a,double b) { return vector(a.x*b,a.y*b); } double dot(vector a,vector b) { return a.x*b.x+a.y*b.y; } double cross(vector a,vector b) { return a.x*b.y-a.y*b.x; } bool inter(point a1,vector b1,point a2,vector b2,point &p) { double a,b,t; vector v=b1-a1,w=b2-a2; if(sig(cross(v,w))==0) return 0; a=cross(v,a2-a1)*cross(v,b2-a1); b=cross(w,a1-a2)*cross(w,b1-a2); if(sig(a)>0 || sig(b)>0) return 0; vector u=a1-a2; t=cross(w,u)/cross(v,w); p=a1+v*t; return 1; } bool onseg(point a,point p,point q) { if(sig(cross(p-a,p-q))==0 && sig(dot(a-p,a-q))<0) return 1; return 0; } int main() { int i,j,k,m,n,c=0; point p[305],q[50000]; while(scanf("%d",&n)!=EOF && n) { for(i=0;i<n;i++) { scanf("%lf%lf",&p[i].x,&p[i].y); q[i]=p[i]; } m=k=0; for(i=0;i<n-3;i++) { for(j=i+2;j<n-1;j++) if(inter(p[i],p[i+1],p[j],p[j+1],q[n+k])) k++; } sort(q,q+n+k); for(i=1;i<n+k;i++) if(q[m]!=q[i]) q[++m]=q[i]; m++; k=n-1; for(i=0;i<m;i++) { for(j=0;j<n-1;j++) if(onseg(q[i],p[j],p[j+1])) k++; } printf("Case %d: There are %d pieces.\n",++c,k-m+2); } return 0; }