题目链接:poj2284 That Nice Euler Circuit

欧拉公式:如果G是一个阶为n,边数为m且含有r个区域的连通平面图,则有恒等式:n-m+r=2。

欧拉公式的推广: 对于具有k(k≥2)个连通分支的平面图G,有:n-m+r=k+1。

题意:给出连通平面图的各顶点,求这个欧拉回路将平面分成多少区域。

题解:根据平面图的欧拉定理“n-m+r=2”来求解区域数r。

顶点个数n:两两线段求交点,每个交点都是图中的顶点。

边数m:在求交点时判断每个交点落在第几条边上,如果一个交点落在一条边上,这条边就分裂成两条边,边数加一。

学习之路漫漫啊。。

看懂了书上例题再敲的,我基础差就是累哦、、

 #include<cstdio>
#include<cmath>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std; const double eps = 1e-;
const int N = ; struct point{//点
double x, y;
point(double _x = , double _y = ): x(_x), y(_y){}
}; struct lineSegment{//线段
point s, e;
lineSegment(point s, point e): s(s), e(e){}
};
struct line{//直线
double a, b, c;
};
bool operator < (point p1, point p2){
return p1.x < p2.x || p1.x == p2.x && p1.y < p2.y;
}
bool operator == (point p1, point p2){
return abs(p1.x - p2.x) < eps && abs(p1.y - p2.y) < eps;
}
bool onLine(lineSegment l, point p){//判断点是否在线段上
return abs((l.e.x - l.s.x)*(p.y - l.s.y) - (p.x - l.s.x)*(l.e.y - l.s.y)) < eps //点在直线上
&& (p.x - l.s.x) * (p.x - l.e.x) < eps && (p.y - l.s.y) * (p.y - l.e.y) < eps;//点在线段内
}
line make_Line(point p1, point p2){//将线段延长为直线
line l;
l.a = (p2.y > p1.y) ? p2.y - p1.y : p1.y - p2.y;
l.b = (p2.y > p1.y) ? p1.x - p2.x : p2.x - p1.x;
l.c = (p2.y > p1.y) ? p1.y * p2.x - p1.x * p2.y : p1.x * p2.y - p1.y * p2.x;
return l;
}
//判断直线是否相交,并求交点p
bool line_Intersect(line l1, line l2, point &p){
double d = l1.a * l2.b - l2.a * l1.b;
if(abs(d) < eps) return false; //叉积为0,平行或重合
p.x = (l2.c * l1.b - l1.c * l2.b) /d;
p.y = (l2.a * l1.c - l1.a * l2.c) /d;
return true;
}
//判断线段是否相交
bool lineSegment_Intersect(lineSegment l1, lineSegment l2, point &p){
line a, b;
a = make_Line(l1.s, l1.e);//将线段延长为直线
b = make_Line(l2.s, l2.e);
if(line_Intersect(a, b, p))//如果直线相交
//判断直线交点是否在线段上,是则线段相交
return onLine(l1, p) && onLine(l2, p);
else return false;
} point p[N], intersection[N];
int n, m; int main(){
int nn, i, j, kase = ;
while(scanf("%d", &nn) && nn){
n = m = ;
for(i = ; i < nn; ++i)
scanf("%lf %lf", &p[i].x, &p[i].y);
for(i = ; i < nn; ++i){
for(j = ; j < nn; ++j){
if(i == j) continue;
lineSegment l1(p[i], p[(i+)%nn]), l2(p[j], p[(j+)%nn]);
point v;
if(lineSegment_Intersect(l1, l2, v))
intersection[n++] = v;//记录交点
}
}
sort(intersection , intersection + n);
//移除重复点
n = unique(intersection, intersection + n) - intersection;
for(i = ; i < n; ++i){
for(j = ; j < nn; ++j){
lineSegment l3(p[j], p[(j+)%nn]);
//若有交点落在边上,则该边分裂成两条边
if(onLine(l3, intersection[i]) && !(l3.s == intersection[i]))
m++;
}
}
printf("Case %d: There are %d pieces.\n", kase++, + m - n);
}
return ;
}

poj2284 That Nice Euler Circuit(欧拉公式)的更多相关文章

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

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

  2. UVa 10735 (混合图的欧拉回路) Euler Circuit

    题意: 给出一个图,有的边是有向边,有的是无向边.试找出一条欧拉回路. 分析: 按照往常的思维,遇到混合图,我们一般会把无向边拆成两条方向相反的有向边. 但是在这里却行不通了,因为拆成两条有向边的话, ...

  3. UVA 10735 Euler Circuit 混合图的欧拉回路(最大流,fluery算法)

    题意:给一个图,图中有部分是向边,部分是无向边,要求判断是否存在欧拉回路,若存在,输出路径. 分析:欧拉回路的定义是,从某个点出发,每条边经过一次之后恰好回到出发点. 无向边同样只能走一次,只是不限制 ...

  4. UVA-10735 - Euler Circuit(混合欧拉回路输出)

    题意:给你一个图,有N个点,M条边,这M条边有的是单向的,有的是双向的. 问你能否找出一条欧拉回路,使得每条边都只经过一次! 分析: 下面转自别人的题解: 把该图的无向边随便定向,然后计算每个点的入度 ...

  5. Uva 1342 - That Nice Euler Circuit

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

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

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

  7. Euler Circuit UVA - 10735(混合图输出路径)

    就是求混合图是否存在欧拉回路 如果存在则输出一组路径 (我就说嘛 咱的代码怎么可能错.....最后的输出格式竟然w了一天 我都没发现) 解析: 对于无向边定向建边放到网络流图中add(u, v, 1) ...

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

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

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

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

随机推荐

  1. c语言中的位移位操作

    先要了解一下C语言里所有的位运算都是指二进制数的位运算.即使输入的是十进制的数,在内存中也是存储为二进制形式. “<<”用法: 格式是:a<<m,a和m必须是整型表达式,要求m ...

  2. hdu 5693 朋友 博弈

    朋友 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Descr ...

  3. JUnit 简单的使用 (学习转载)

    JUnit4使用Java5中的注解(annotation),以下是JUnit4常用的几个annotation: @Before:初始化方法   对于每一个测试方法都要执行一次(注意与BeforeCla ...

  4. [转]-Android Studio 快捷键整理分享-SadieYu

    文章编辑整理:Android Studio 中文组 - SadieYu Alt+回车 导入包,自动修正 Ctrl+N   查找类 Ctrl+Shift+N 查找文件 Ctrl+Alt+L  格式化代码 ...

  5. iOS - UINavigationController

    前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UINavigationController : UIViewController @available(iOS 2 ...

  6. new road

    9月底进入到了新的公司,有些类似实习的时候的路程. 新公司的数据业务基本是一种幻想状态,完全没有什么数据方面的积淀.

  7. 控制反转和spring在项目中可以带来的好处

    Spring实例化Bean的三种方式分别是: 1,xml配置使用bean的类构造器 <bean id="personService" class="cn.servi ...

  8. (一)使用springAPI以及自定义类 实现AOP-aop编程

    Spring的另一个重要思想是AOP,面向切面的编程,它提供了一种机制,可以在执行业务前后执行另外的代码,Servlet中的Filter就是一种AOP思想的体现,下面通过一个例子来感受一下. 假设我们 ...

  9. 自定义获取焦点的TextView

    自定义控件编写流程 创建一个默认就能获取焦点的TextView 1.创建一个类继承至TextView,FocusTextView 2.重写其构造方法 //使用在通过java代码创建控件 public ...

  10. unsigned 整型实现无溢出运算

    普通的 int 整型能表示的范围很有限,所以刷题时很多时候不得不用 long long 来存更大的数据.或者找出数列中某个只出现一次(或奇数次)的数(其余的数均出现两次 / 偶数次),用异或运算的经典 ...