题意:给出一个一笔画图形所经过的端点,最后一个点正好是出发点(共有n个点,n<=300)
问平面被分成了多少部分
欧拉定理:
平面上顶点数 v,边数 e,面数 f
v+f-e=2
所以f=e+2-v
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)
#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)
typedef long long ll;
typedef pair<int, int> pii;
const int INF =0x3f3f3f3f;
const int maxn= 300 ;
const double eps=1e-10;
struct Point
{
double x,y;
Point(double x=0,double y=0):x(x),y(y) {};
}p[maxn+10],V[maxn*maxn+10];
int dcmp(double x)
{
if(fabs(x)<eps) return 0;
else return x<0?-1:1;
}
bool operator<(const Point &a ,const Point& b)
{
return a.x<b.x||(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;
}
typedef Point Vector;
Vector operator +(Vector A,Vector B) {return Vector(A.x+B.x,A.y+B.y); }
Vector operator -(Vector A,Vector B) {return Vector(A.x-B.x,A.y-B.y); }
Vector operator *(Vector A,double p) {return Vector(A.x*p,A.y*p); }
Vector operator /(Vector A,double p) {return Vector(A.x/p,A.y/p); }
Vector operator -(Vector A) {return Vector(-A.x,-A.y);}
double Cross(Vector A,Vector B)//叉乘
{
return A.x*B.y-A.y*B.x;
}
double Dot(Vector A,Vector B)//点乘
{
return A.x*B.x+A.y*B.y;
}
bool SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2)//规范相交
{
double c1=Cross(a2-a1,b1-a1),c2=Cross(a2-a1,b2-a1),
c3=Cross(b2-b1,a1-b1),c4=Cross(b2-b1,a2-b1);
return dcmp(c1)*dcmp(c2)<0&&dcmp(c3)*dcmp(c4)<0;
}
Point GetLineIntersection(Point P,Vector v,Point Q,Vector w)
{
Vector u=P-Q;
double t=Cross(w,u)/Cross(v,w);
return P+v*t;
}
bool onSegment(Point p,Point a1,Point a2)
{
return dcmp(Cross(a1-p,a2-p) )==0&&dcmp(Dot(a1-p,a2-p) )<0;
}
/**
欧拉定理:
平面上顶点数 v,边数 e,面数 f
v+f-e=2
所以f=e+2-v
**/
int n,v,e,f;
void work()
{
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(SegmentProperIntersection(p[i],p[i+1],p[j],p[j+1]))
V[v++]=GetLineIntersection(p[i],p[i+1]-p[i],p[j],p[j+1]-p[j]);
}
}
sort(V,V+v);
v=unique(V,V+v)-V;//重新用V储存所有的点,目的就是保留原来的p,保留原来的线段
for(int i=0;i<v;i++)
{//原来的点也可能是(在某条线段上(不包含端点))
for(int j=0;j<n ;j++)
{
if(onSegment(V[i],p[j],p[j+1]) ) e++;
}
}
f=e+2-v;
}
int main()
{
int kase=0;
while(~scanf("%d",&n)&&n)
{
for0(i,n)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
V[i]=p[i];
}
n--;
v=e=n;
work();
// cout<<e<<" "<<v<<endl;
printf("Case %d: There are %d pieces.\n",++kase,f);
}
return 0;
}
/*
6
-2 0 -1 -1 0 0 1 -1 2 0 -2 0
*/