首页 > 其他 > 详细

UVALA 3263 That Nice Euler Circuits(欧拉定理,判断线段相交)

时间:2015-03-27 14:54:25      阅读:294      评论:0      收藏:0      [点我收藏+]

解题思路:

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

本题要求平面数,即 F = E + 2 - V;

因此只需要求出顶点数和边数。顶点数除了输入的顶点还包括两条线段相交的交点,同样如果三点共线,则原来的一条边变成了两条边。

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#define LL long long
using namespace std;
const int MAXN = 300 + 10;
struct Point
{
    double x, y;
    Point (double x = 0, double y = 0) : x(x), y(y) { }
};
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); }
const double eps = 1e-10;
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 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 SegmentIntersection(Point a1, Point a2, Point b1, Point b2)
{
    double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1);
    double 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;
}
Point P[MAXN], V[MAXN*MAXN];
int main()
{
    int n, kcase = 1;
    while(scanf("%d", &n)!=EOF)
    {
        if(n == 0) break;
        for(int i=0;i<n;i++)
        {
            scanf("%lf%lf", &P[i].x, &P[i].y);
            V[i] = P[i];
        }
        n--;
        int c = n, e = n;
        for(int i=0;i<n;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                if(SegmentIntersection(P[i], P[i+1], P[j], P[j+1]))
                   V[c++] = GetLineIntersection(P[i], P[i+1]-P[i], P[j], P[j+1]-P[j]);
            }
        }
        sort(V, V+c);
        c = unique(V, V+c) - V;
        for(int i=0;i<c;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(OnSegment(V[i], P[j], P[j+1])) e++;
            }
        }
        printf("Case %d: There are %d pieces.\n", kcase++, e + 2 - c);
    }
    return 0;
}



UVALA 3263 That Nice Euler Circuits(欧拉定理,判断线段相交)

原文:http://blog.csdn.net/moguxiaozhe/article/details/44675123

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!