Uvalive 3263 That Nice Euler Circuit(几何欧拉定理)

62 篇文章 0 订阅
11 篇文章 0 订阅
题目地址:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1264

思路:欧拉定理:设平面图的顶点数、边数和面数分别为V、E和F,则V+F-E=2。

顶点数可以通过计算两线段交点计算:首先判断两线段是否相交(每条线段的两个端点均在另一条线段的两侧),若相交,则按照求两直线交点方式求出两线段交点。由于可能有多于两条线段交于一点,所以交点需判重。

边数可以根据所求顶点,枚举每条边,判断顶点是否位于该条边上(非端点位置)。若有一点位于边上,则每次将该条线段分隔为两部分,边数加一。

#include<cmath>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define debug
using namespace std;
const double eps=1e-10;


struct Point
{
    double x,y;
    Point(double x=0,double y=0):x(x),y(y) {}
    void read()
    {
        scanf("%lf%lf",&x,&y);
    }
    void print()
    {
      printf("%.6f %.6f",x,y);
    }
};

typedef Point Vector;

Vector operator + (Vector A,Vector B)
{
    return Vector(A.x+B.x,A.y+B.y);
}

Vector operator - (Point A,Point 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);
}

bool operator < (const Point &a,const Point &b)
{
    return a.x<b.x||(a.x==b.x&&a.y<b.y);
}

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 dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;
}

double Dot(Vector A,Vector B)
{
    return A.x*B.x+A.y*B.y;
}

double Length(Vector A)
{
    return sqrt(Dot(A,A));
}

double Angle(Vector A,Vector B)
{
    return acos(Dot(A,B)/Length(A)/Length(B));
}

Vector Rotate(Vector A,double rad)
{
    return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));
}

double Cross(Vector A,Vector B)
{
    return A.x*B.y-A.y*B.x;
}

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

bool OnSegment(Point p,Point a1,Point a2)
{
  return dcmp(Cross(a1-p,a2-p))==0&&dcmp(Dot(a1-p,a2-p))<0;
}

const int maxn=300+50;

int n;
Point p[maxn];
vector<Point> totp;

int main()
{
#ifdef debu
    freopen("in.txt","r",stdin);
#endif // debug
    int cas=0;
    while(scanf("%d",&n)!=EOF&&n)
    {
      int numl=n-1;
      totp.clear();
      for(int i=1;i<=n;i++)
      {
        p[i].read();
        totp.push_back(p[i]);
      }
      for(int i=1;i<n;i++)
      {
        for(int j=i+1;j<n;j++)
        {
          if(SegmentProperIntersection(p[i],p[i+1],p[j],p[j+1]))
          {
            totp.push_back(GetLineIntersection(p[i],p[i+1]-p[i],p[j],p[j+1]-p[j]));
          }
        }
      }
      sort(totp.begin(),totp.end());
      int nump=unique(totp.begin(),totp.end())-totp.begin();
      for(int i=0;i<nump;i++)
      {
        for(int j=1;j<n;j++)
        {
          if(OnSegment(totp[i],p[j],p[j+1]))   //判断点是否在一条线段上(不含端点)
           {
             numl++;
           }
        }
      }
      printf("Case %d: There are %d pieces.\n",++cas,numl-nump+2);
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值