uvalive 3263

题意:平面上有一个包含n个端点的一笔画,第n个端点和第一个端点重合,因此图案是一个闭合曲线,组成的线段可以相交但不重叠,问这些线段将平面切成多少部分。
注意输入的时候输入了n+1个点,最后一个点是起点。
题解:欧拉定理:平面图的顶点数V,边数E,面数F的关系是 V + F - E = 2
有了这个定理,计算一下新增的点(任意两边求交点),然后每个新增的点在现有的线段上就新增一条边,注意点判重。

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
///          F = E + 2 - V
const int N = 305;
struct Point {
    double x, y;
    Point(double x = 0, double y = 0): x(x), y(y) {}
}p[N], v[N * N];
int n;

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

Point operator - (Point A, Point B) {
    return Point(A.x - B.x, A.y - B.y);
}

Point operator * (Point A, double p) {
    return Point(A.x * p, A.y * p);
}

Point operator / (Point A, double p) {
    return Point(A.x / p, A.y / p);
}
//计算点积的正负  负值夹角为钝角
int dcmp(double x) {
    if (fabs(x) < 1e-9)
        return 0;
    return x < 0 ? -1 : 1;
}

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;
}
//计算点积
double Dot(Point A, Point B) {
    return A.x * B.x + A.y * B.y;
}
//计算叉积,也就是数量积
double Cross(Point A, Point B) {
    return A.x * B.y - A.y * B.x;
}
//计算向量长度
double Length(Point A) {
    return sqrt(Dot(A, A));
}
//向量A旋转rad弧度,rad负值为顺时针旋转
Point Rotate(Point A, double rad) {
    return Point(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad));
}
//得到两直线交点 
Point GetLineIntersection(Point P, Point v, Point Q, Point w) {
    Point 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);
    double c2 = Cross(a2 - a1, b2 - a1);
    double c3 = Cross(b2 - b1, a1 - b1);
    double c4 = Cross(b2 - b1, a2 - b1);
    return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0;
}
//判断点p是否在线段a1--a2上(不包括端点)
bool OnSegment(Point p, Point a1, Point a2) {
    return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 - p)) < 0;
}

int main() {
    int cas = 1;
    while (scanf("%d", &n) == 1 && n) {
        for (int i = 0; i < n; i++) {
            scanf("%lf%lf", &p[i].x, &p[i].y);
            v[i] = p[i];
        }
        n--; //最后一个是起点
        int E = n, V = n;
        for (int i = 0; i < n; i++)
            for (int j = i + 1; j < n; j++)
                if (SegmentProperIntersection(p[i], p[i + 1], p[j], p[j + 1]))
                    v[V++] = GetLineIntersection(p[i], p[i + 1] - p[i], p[j], p[j + 1] - p[j]);
        sort(v, v + V);
        V = unique(v, v + V) - v;
        for (int i = 0; i < V; 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", cas++, E + 2 - V);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值