• UVa1342 That Nice Euler Circuit (计算几何)


    题目链接:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=446&page=show_problem&problem=4088

    使用平面图的欧拉定理,(V+F-E=2),所以问题就变成求出有多少个顶点和边

    枚举线段,判段线段两两是否产生新的交点(端点相交不算),可能出现三线共点的情况,所以还需要去重

    原来的边上每新增一个点,则边数就加一,判断新增点是否在原来的线段上即可

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    
    const int maxn = 310; 
    
    const double eps = 1e-10;
    int dcmp(double x) {
      if(fabs(x) < eps) return 0; else return x < 0 ? -1 : 1;
    }
    
    struct Point {
      double x, y;
      Point(double x=0, double y=0):x(x),y(y) { }
    };
    
    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 + (const Vector& A, const Vector& B) { return Vector(A.x+B.x, A.y+B.y); }
    Vector operator - (const Point& A, const Point& B) { return Vector(A.x-B.x, A.y-B.y); }
    Vector operator * (const Vector& A, double p) { return Vector(A.x*p, A.y*p); }
    double Dot(const Vector& A, const Vector& B) { return A.x*B.x + A.y*B.y; }
    double Length(const Vector& A) { return sqrt(Dot(A, A)); }
    double Angle(const Vector& A, const Vector& B) { return acos(Dot(A, B) / Length(A) / Length(B)); }
    double Cross(const Vector& A, const Vector& B) { return A.x*B.y - A.y*B.x; }
    
    bool SegmentProperIntersection(const Point& a1, const Point& a2, const Point& b1, const 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(const Point& P, const Point& v, const Point& Q, const Point& w) {
      Vector u = P-Q;
      double t = Cross(w, u) / Cross(v, w);
      return P+v*t;
    }
    
    Vector Rotate(const Vector& A, double rad) {
      return Vector(A.x*cos(rad)-A.y*sin(rad), A.x*sin(rad)+A.y*cos(rad));
    }
    
    bool OnSegment(const Point& p, const Point& a1, const Point& a2) {
      return dcmp(Cross(a1-p, a2-p)) == 0 && dcmp(Dot(a1-p, a2-p)) < 0;
    }
    
    int n, tot, e;
    Point p[maxn], v[maxn*maxn];
    
    ll read(){ ll s = 0, f = 1; char ch = getchar(); while(ch < '0' || ch > '9'){ if(ch == '-') f = -1; ch = getchar(); } while(ch >= '0' && ch <= '9'){ s = s * 10 + ch - '0'; ch = getchar(); } return s * f; }
    
    int main(){
    	int kase = 0;
    	while(scanf("%d", &n) == 1 && n){
    		tot = 0;
    		for(int i = 1 ; i <= n ; ++i){
    			scanf("%lf%lf", &p[i].x, &p[i].y);
    			v[i] = p[i];
    		}
    		--n;
    		tot = n; e = n;
    		
    		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])){
    					v[++tot] = GetLineIntersection(p[i], p[i+1]-p[i], p[j], p[j+1]-p[j]);
    				}
    			}
    		}
    		
    		sort(v+1, v+1+tot);
    		tot = unique(v+1, v+1+tot) - v - 1;
    		
    		for(int i = 1 ; i <= tot ; ++i){
    			for(int j = 1 ; j <= n ; ++j){
    				if(OnSegment(v[i], p[j], p[j+1])) ++e;
    			}
    		}
    		
    		printf("Case %d: There are %d pieces.
    ", ++kase, e+2-tot);
    	}
    	return 0;
    }
    
  • 相关阅读:
    K好数
    最大最小公倍数
    十六进制转十进制
    利用malloc定义数组
    01字串
    ubuntu 14.04 下jdk和sdk+eclipse 的配置
    Mysql3
    求最大连续子串
    UC笔试
    java实现随机洗牌算法
  • 原文地址:https://www.cnblogs.com/tuchen/p/15204798.html
Copyright © 2020-2023  润新知