题意:

平面上有n个端点的一笔画,最后一个端点与第一个端点重合,即所给图案是闭合曲线。求这些线段将平面分成多少部分。

分析:

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

所求结果不容易直接求出,因此我们可以转换成 F=E-V+2

枚举两条边,如果有交点则顶点数+1,并将交点记录下来

所有交点去重(去重前记得排序),如果某个交点在线段上,则边数+1

 //#define LOCAL
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std; const int maxn = + ; struct Point
{
double x, y;
Point(double x=, double y=) :x(x),y(y) {}
};
typedef Point Vector;
const double EPS = 1e-; 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); } 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 ;
else return x < ? - : ; } bool operator == (const Point& a, const Point& b)
{ return dcmp(a.x-b.x) == && dcmp(a.y-b.y) == ; } double Dot(Vector A, Vector B)
{ return A.x*B.x + A.y*B.y; } double Length(Vector A) { return sqrt(Dot(A, A)); } double Angle(Vector A, Vector B)
{ return acos(Dot(A, B) / Length(A) / Length(B)); } double Cross(Vector A, Vector B)
{ return A.x*B.y - A.y*B.x; } double Area2(Point A, Point B, Point C)
{ return Cross(B-A, C-A); } Vector VRotate(Vector A, double rad)
{
return Vector(A.x*cos(rad) - A.y*sin(rad), A.x*sin(rad) + A.y*cos(rad));
} Point PRotate(Point A, Point B, double rad)
{
return A + VRotate(B-A, rad);
} Vector Normal(Vector A)
{
double l = Length(A);
return Vector(-A.y/l, A.x/l);
} 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;
}
double DistanceToLine(Point P, Point A, Point B)
{
Vector v1 = B - A, v2 = P - A;
return fabs(Cross(v1, v2)) / Length(v1);
} double DistanceToSegment(Point P, Point A, Point B)
{
if(A == B) return Length(P - A);
Vector v1 = B - A, v2 = P - A, v3 = P - B;
if(dcmp(Dot(v1, v2)) < ) return Length(v2);
else if(dcmp(Dot(v1, v3)) > ) return Length(v3);
else return fabs(Cross(v1, v2)) / Length(v1);
} Point GetLineProjection(Point P, Point A, Point B)
{
Vector v = B - A;
return A + v * (Dot(v, P - A) / Dot(v, v));
} bool SegmentProperIntersection(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)< && dcmp(c3)*dcmp(c4)<;
} bool OnSegment(Point P, Point a1, Point a2)
{
Vector v1 = a1 - P, v2 = a2 - P;
return dcmp(Cross(v1, v2)) == && dcmp(Dot(v1, v2)) < ;
} Point P[maxn], V[maxn*maxn]; int main(void)
{
#ifdef LOCAL
freopen("3263in.txt", "r", stdin);
#endif int n, kase = ;
while(scanf("%d", &n) == && n)
{
for(int i = ; 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 = ; i < n; ++i)
for(int j = i+; j < n; ++j)
if(SegmentProperIntersection(P[i], P[i+], P[j], P[j+]))
V[c++] = GetLineIntersection(P[i], P[i+]-P[i], P[j], P[j+]-P[j]); sort(V, V+c);
c = unique(V, V+c) - V; for(int i = ; i < c; ++i)
for(int j = ; j < n; ++j)
if(OnSegment(V[i], P[j], P[j+])) e++; printf("Case %d: There are %d pieces.\n", ++kase, e+-c);
} return ;
}

代码君

LA 3263 (平面图的欧拉定理) That Nice Euler Circuit的更多相关文章

  1. LA 3263 That Nice Euler Circuit(欧拉定理)

    That Nice Euler Circuit Little Joey invented a scrabble machine that he called Euler, after the grea ...

  2. UVALive - 3263 That Nice Euler Circuit (几何)

    UVALive - 3263 That Nice Euler Circuit (几何) ACM 题目地址:  UVALive - 3263 That Nice Euler Circuit 题意:  给 ...

  3. UVALi 3263 That Nice Euler Circuit(几何)

    That Nice Euler Circuit [题目链接]That Nice Euler Circuit [题目类型]几何 &题解: 蓝书P260 要用欧拉定理:V+F=E+2 V是顶点数; ...

  4. LA 3263 平面划分

    Little Joey invented a scrabble machine that he called Euler, after the great mathematician. In his ...

  5. poj2284 That Nice Euler Circuit(欧拉公式)

    题目链接:poj2284 That Nice Euler Circuit 欧拉公式:如果G是一个阶为n,边数为m且含有r个区域的连通平面图,则有恒等式:n-m+r=2. 欧拉公式的推广: 对于具有k( ...

  6. POJ2284 That Nice Euler Circuit (欧拉公式)(计算几何 线段相交问题)

                                                          That Nice Euler Circuit Time Limit: 3000MS   M ...

  7. That Nice Euler Circuit(LA3263+几何)

    That Nice Euler Circuit Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu D ...

  8. poj 2284 That Nice Euler Circuit 解题报告

    That Nice Euler Circuit Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 1975   Accepted ...

  9. ●POJ 2284 That Nice Euler Circuit

    题链: http://poj.org/problem?id=2284 题解: 计算几何,平面图的欧拉定理 欧拉定理:设平面图的定点数为v,边数为e,面数为f,则有 v+f-e=2 即 f=e-v+2 ...

随机推荐

  1. ProgressDialog弹出时的底色变暗(转)

    背景:在做一个进度条时,不想让它的背景变暗,以免影响其他区域的正常显示. 在网上搜索时,看到的方法多数是: 方法一 :在代码中 可以这么设置 Window mWindow = getWindow(); ...

  2. windows最基本命令行

    7:计算机运行命令全集 winver---------检查Windows版本 wmimgmt.msc----打开windows管理体系结构 wupdmgr--------windows更新程序 win ...

  3. C# Winform 拖放操作

    http://www.cnblogs.com/imlions/p/3189773.html 在开发程序的时候,为了提高用户的使用体验,或满足相关用户的功能,总是离不开拖放功能.而本文是总结winfor ...

  4. HDOJ 1284 钱币兑换问题

    转自:wutianqi http://www.wutianqi.com/?p=981 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1284 tag:母 ...

  5. POJ2217 Secretary 后缀数组&&高度数组

    学后缀数组后的一道裸题.先来讲讲收获,作为字符串初学者,后缀数组也是刚刚在学,所幸的是有一篇好的论文<后缀数组--处理字符串的有力工具>by 罗穗骞,里面非常详尽地介绍了有关后缀数组的概念 ...

  6. C#委托及事件

    转载:http://www.cnblogs.com/warensoft/archive/2010/03/19/1689806.html C#委托及事件 在C#中,委托(delegate)是一种引用类型 ...

  7. ifram一些常用的知识点

    本文摘自:http://www.cnblogs.com/duankaige/archive/2012/09/20/2695012.html   iframe的调用包括以下几个方面:(调用包含html ...

  8. 集合、拆箱、装箱、自定义集合的foreach

    集合部分 参考:http://msdn.microsoft.com/zh-cn/library/0ytkdh4s(v=vs.110).aspx 集合类型是诸如哈希表.队列.堆栈.包.字典和列表等数据集 ...

  9. 安装SQL Server 2012遇到“需要更新的以前的Visual Studio 2010实例.”

    Microsoft Visual Studio 2010 Service Pack 1(exe) 下载链接:http://www.microsoft.com/zh-cn/download/confir ...

  10. MQTT客户端与服务代理的案列

    服务端,采用 Mosquitto 来转发分发消息. 客户端自己写. 服务端 启动 mosquitto (底下的命令是我自己放到环境变量里面的,通过alias 运行mosquitto) Ishallbe ...