LA 3263 (平面图的欧拉定理) That Nice Euler Circuit
题意:
平面上有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的更多相关文章
- LA 3263 That Nice Euler Circuit(欧拉定理)
That Nice Euler Circuit Little Joey invented a scrabble machine that he called Euler, after the grea ...
- UVALive - 3263 That Nice Euler Circuit (几何)
UVALive - 3263 That Nice Euler Circuit (几何) ACM 题目地址: UVALive - 3263 That Nice Euler Circuit 题意: 给 ...
- UVALi 3263 That Nice Euler Circuit(几何)
That Nice Euler Circuit [题目链接]That Nice Euler Circuit [题目类型]几何 &题解: 蓝书P260 要用欧拉定理:V+F=E+2 V是顶点数; ...
- LA 3263 平面划分
Little Joey invented a scrabble machine that he called Euler, after the great mathematician. In his ...
- poj2284 That Nice Euler Circuit(欧拉公式)
题目链接:poj2284 That Nice Euler Circuit 欧拉公式:如果G是一个阶为n,边数为m且含有r个区域的连通平面图,则有恒等式:n-m+r=2. 欧拉公式的推广: 对于具有k( ...
- POJ2284 That Nice Euler Circuit (欧拉公式)(计算几何 线段相交问题)
That Nice Euler Circuit Time Limit: 3000MS M ...
- That Nice Euler Circuit(LA3263+几何)
That Nice Euler Circuit Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu D ...
- poj 2284 That Nice Euler Circuit 解题报告
That Nice Euler Circuit Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 1975 Accepted ...
- ●POJ 2284 That Nice Euler Circuit
题链: http://poj.org/problem?id=2284 题解: 计算几何,平面图的欧拉定理 欧拉定理:设平面图的定点数为v,边数为e,面数为f,则有 v+f-e=2 即 f=e-v+2 ...
随机推荐
- 在eclipse里的 flex 没有可视化的编辑
注:在4.7版本里去掉了可视化编辑器. 转自:http://3470973.blog.51cto.com/3460973/1135328 最近eclipse切换了一个工作空间,创建的flex项 ...
- switch..case函数的基础使用一
基本作用:switch中的参数与case的值进行比对,相等则进入case. JDK1.7 switch支持int.Integer.String类型 package com.my.test; impor ...
- InterlliJ调试:Method breakpoints may dramatically slow down debugging
问题:Method breakpoints may dramatically slow down debugging 因为此问题久久不能调试.问题发现后原来如此... 原因:设置了方法断点!--什么是 ...
- 【BZOJ】【2194】快速傅里叶之二
FFT c[k]=sigma a[i]*b[i-k] 这个形式不好搞…… 而我们熟悉的卷积的形式是这样的 c[k]=sigma a[i]*b[k-i]也就是[下标之和是定值] 所以我们将a数组反转一下 ...
- spoj 78
数学 组合 隔板法 #include <iostream> #include <cstring> #include <cstdio> #include <s ...
- Kafka之Purgatory Redesign Proposal (翻译)
Purgatory是Kafka server中处理请求时使用的一个重要的数据结构.正好研究ReplicaManager源码的时候发现了这篇文章,顺便翻译下.由于这个proposal里的很多东西需要看源 ...
- 【redis】02string类型和hash类型
Redis的数据类型 Redis主要分为五个数据类型,一个是string,最简单的一个数据类型,hash,list, 还有set集合,还有zset有序集合,这是咱们redis的五种基础类型, 接下 ...
- zoj 3745 Salary Increasing(坑爹的细节题!)
题目 注意题目中的,引用绝望的乐园中的进一步解释如下: 这是一道浙大月赛的题,一如既往的坑爹,好好一道水题,被搞成一道坑题!!! //注意:r(i) < l(i+1) !细节啊细节! #incl ...
- VNC Server 配置
1. 检查vnc客户端和服务器是否已经安装: [gavin@centos ~]$ rpm -q vnc vnc-server package vnc is not installed vnc-serv ...
- [主席树]HDOJ4348 To the moon
题意:n个数, m个操作 1. C l r d 给[l, r]区间的每个数加上d2. Q l r: 查询[l, r]区间的和3. H l r t: 查询第t个操作时[l, r]区间的和4. B ...