题目链接:UVa 1342 That Nice Euler Circuit
几何。
欧拉定理,设平面图的顶点数、边数、和面数分别为V,E和F,则V+F-E=2。
然后求出来顶点数和边数就可以了。
主要还是用了白书上的几何模版,很好很强大。
#include <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;
const double eps = 1e-10;
const int MAX_N = 300 + 10;
struct Point
{
double x, y;
Point(double x=0, double y=0):x(x),y(y) { }
};
typedef Point Vector;
Vector operator + (const Vector& A, const Vector& B)
{
return Vector(A.x+B.x, A.y+B.y);
}
Vector operator - (const Point& A, const Point& B)
{
return Vector(A.x-B.x, A.y-B.y);
}
Vector operator * (const 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 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(const Vector& A, const Vector& B)
{
return A.x*B.x + A.y*B.y;
}
double Length(const Vector& A)
{
return sqrt(Dot(A, A));
}
double Angle(const Vector& A, const Vector& B)
{
return acos(Dot(A, B) / Length(A) / Length(B));
}
double Cross(const Vector& A, const Vector& B)
{
return A.x*B.y - A.y*B.x;
}
bool SegmentProperIntersection(const Point& a1, const Point& a2, const Point& b1, const Point& b2)
{
double c1 = Cross(a2-a1,b1-a1), c2 = Cross(a2-a1,b2-a1),
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(const Point& p, const Point& a1, const Point& a2)
{
return dcmp(Cross(a1-p, a2-p)) == 0 && dcmp(Dot(a1-p, a2-p)) < 0;
}
Point GetLineIntersection(const Point& P, const Point& v, const Point& Q, const Point& w)
{
Vector u = P-Q;
double t = Cross(w, u) / Cross(v, w);
return P+v*t;
}
Vector Rotate(const Vector& A, double rad)
{
return Vector(A.x*cos(rad)-A.y*sin(rad), A.x*sin(rad)+A.y*cos(rad));
}
Point p[MAX_N], v[MAX_N * MAX_N];
int n, cnt;
int main()
{
cnt = 0;
while(scanf("%d", &n), n)
{
for(int i = 0; i < n; i++)
{
scanf("%lf%lf", &p[i].x, &p[i].y);
v[i] = p[i];
}
n--;
int c, e;
c = e = 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[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;//返回不重复元素的数目
/**添加边
添加新边,OnSegment判断点v[i]是否在p[j]和p[j + 1]组成的线段上,不包括端点。
枚举的边都是原有边,
那么如果一个点在线段上,这个点就把这条线段分成了两段,所以边数需要加1,
如果这个点是这条边的端点,边数不需要加1,其实就是说明这个点是原有点,而原有点不会分割原有线段
这个逻辑也正符合OnSegment的判定规则。
**/
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", ++cnt, e - c + 2);
}
return 0;
}