uva 1342 - That Nice Euler Circuit

題意:平面上有一個包含n個點的一筆畫,圖案是封閉的。線段可以相交,但不會部分重疊,如圖,求平面被分成多少部分。

\epsfbox{p3263.eps}

#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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章