That Nice Euler Circuit UVA - 1342(平面图的欧拉定理)

#include<bits/stdc++.h>
using namespace std;

#define rep(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=b-1;i>=a;i--)

const double eps=1e-6;
const double pi=acos(-1.0);
//可以在Point里面用Vec了
class Point;
typedef Point Vec;

//三态函数比较;精度问题
int dcmp(double x){
    if(fabs(x)<eps) return 0;
    return x<0?-1:1;
}
struct Point{
    double x,y;
    Point(double _x=0,double _y=0):x(_x),y(_y){}

    /*
     *向量运算
     */
    //向量与常数 注意常数要放在后面
    Vec operator*(double p){
        return Vec(x*p,y*p);
    }
    Vec operator/(double p){
        return Vec(x/p,y/p);
    }

    //向量与向量
    Vec operator-(Vec obj){
        return Vec(x-obj.x,y-obj.y);
    }
    Vec operator+(Vec obj){
        return Vec(x+obj.x,y+obj.y);
    }

    //点积
    double operator*(Vec obj){
        return x*obj.x+y*obj.y;
    }
    //叉积
    double operator^(Vec obj){
        return x*obj.y-y*obj.x;
    }

    //两个向量的夹角 A*B=|A|*|B|*cos(th)
    double Angle(Vec B){
        return acos((*this)*B/(*this).len()/B.len());
    }
    //两条向量平行四边形的面积
    double Area(Vec B){
        return fabs((*this)^B);//
    }

    //向量旋转
    //旋转公式
    //  Nx    (cos  -sin)  x
    //      =
    //  Ny    (sin   cos)  y
    Vec Rotate(double rad){
        return Vec(x*cos(rad)-y*sin(rad),x*sin(rad)+y*cos(rad));
    }

    //返回向量的法向量,即旋转pi/2
    Vec Normal(){
        //返回单位法向量,注意L不能为0
        /*
        double L=obj.len();
        return Vec(-y/L,x/L);
        */
        //返回法向量
        return Vec(-y,x);
    }


    /*
     * 向量的性质
     */
    //返回向量的长度,或者点距离原点的距离
    double len(){
        return hypot(x,y);
    }
    //返回两点之间的距离
    double dis(Point obj){
        //return hypot(x-obj.x,y-obj.y); //hypot 给定直角三角形的两条直角边,返回斜边边长
        return sqrt((x-obj.x)*(x-obj.x)+(y-obj.y)*(y-obj.y));
    }
    //向量的极角 atan2(y,x)

    /*
     *向量的关系
     */
     bool operator==(Point obj){
        return dcmp(x-obj.x)==0&&dcmp(y-obj.y)==0;
     }
     bool operator<(Point obj){
        return x<obj.x||(x==obj.x&&y<obj.y);
     }
};

const int maxn=310;
Point p[maxn],pp[maxn*maxn];

bool SI(Point a1,Point a2,Point b1,Point b2){
    double c1=(a2-a1)^(b1-a1),c2=(a2-a1)^(b2-a1);
    double c3=(b2-b1)^(a1-b1),c4=(b2-b1)^(a2-b1);
    return dcmp(c1)*dcmp(c2)<0&&dcmp(c3)*dcmp(c4)<0;
}

Point GI(Point P,Vec v,Point Q,Vec w){
    Vec u=P-Q;
    double t=(w^u)/(v^w);
    return P+v*t;
}

bool OS(Point p,Point a1,Point a2){
    return dcmp((a1-p)^(a2-p))==0&&dcmp((a1-p)*(a2-p))<0;
}
/*
5
0 0 0 1 1 1 1 0 0 0
7
1 1 1 5 2 1 2 5 5 1 3 5 1 1
5
0 0 1 0 2 0 2 1 0 0
0
最后一个和第一个运算: 在最后加一个起点。有点像字符串

对于三点共线的情况,我们可以知道 加一个点,就多一条边,所以最后没有影响。
只是哪些交点必须要计算

!!!Attention  如果多了点,我们要计算到底需要多少的空间,防止RE
*/
int main(){
    int n,kase=0;
    while(scanf("%d",&n)==1&&n){
      //  int cnt=0;
        rep(i,0,n){

            scanf("%lf %lf",&p[i].x,&p[i].y),pp[i]=p[i];
        }
        n--;
        int v=n,e=n;
        int cnt=n;
        rep(i,0,n){
            rep(j,i+1,n){
                if(SI(p[i],p[i+1],p[j],p[j+1])){
                    pp[cnt++]=GI(p[i],p[i+1]-p[i],p[j],p[j+1]-p[j]);
                }
            }
        }
        sort(pp,pp+cnt);
        cnt=unique(pp,pp+cnt)-pp;

        //printf("cnt:%d\n",cnt);

        rep(i,0,cnt){
            rep(j,0,n){
                if(OS(pp[i],p[j],p[j+1]))e++;
            }
        }
        printf("Case %d: There are %d pieces.\n",++kase,e+2-cnt);
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值