uvaLive 3263 That Nice Euler Circuit 欧拉定理

题意:给出一个一笔画图形所经过的端点,最后一个点正好是出发点(共有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
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值