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

本文介绍了一种利用欧拉定理计算由线段构成的平面图中面的数量的方法。通过分析顶点、边及它们之间的相交情况,结合平面几何中的交点计算与线段交集判断等技巧,实现了对平面图划分数量的有效求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

解题思路:

欧拉定理: 设平面图的顶点数,边数和面数分别为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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值