poj2284 That Nice Euler Circuit(欧拉公式)
题目链接: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(欧拉公式)的更多相关文章
- POJ2284 That Nice Euler Circuit (欧拉公式)(计算几何 线段相交问题)
That Nice Euler Circuit Time Limit: 3000MS M ...
- UVa 10735 (混合图的欧拉回路) Euler Circuit
题意: 给出一个图,有的边是有向边,有的是无向边.试找出一条欧拉回路. 分析: 按照往常的思维,遇到混合图,我们一般会把无向边拆成两条方向相反的有向边. 但是在这里却行不通了,因为拆成两条有向边的话, ...
- UVA 10735 Euler Circuit 混合图的欧拉回路(最大流,fluery算法)
题意:给一个图,图中有部分是向边,部分是无向边,要求判断是否存在欧拉回路,若存在,输出路径. 分析:欧拉回路的定义是,从某个点出发,每条边经过一次之后恰好回到出发点. 无向边同样只能走一次,只是不限制 ...
- UVA-10735 - Euler Circuit(混合欧拉回路输出)
题意:给你一个图,有N个点,M条边,这M条边有的是单向的,有的是双向的. 问你能否找出一条欧拉回路,使得每条边都只经过一次! 分析: 下面转自别人的题解: 把该图的无向边随便定向,然后计算每个点的入度 ...
- Uva 1342 - That Nice Euler Circuit
Little Joey invented a scrabble machine that he called Euler, after the great mathematician. In his ...
- UVALive - 3263 That Nice Euler Circuit (几何)
UVALive - 3263 That Nice Euler Circuit (几何) ACM 题目地址: UVALive - 3263 That Nice Euler Circuit 题意: 给 ...
- Euler Circuit UVA - 10735(混合图输出路径)
就是求混合图是否存在欧拉回路 如果存在则输出一组路径 (我就说嘛 咱的代码怎么可能错.....最后的输出格式竟然w了一天 我都没发现) 解析: 对于无向边定向建边放到网络流图中add(u, v, 1) ...
- That Nice Euler Circuit(LA3263+几何)
That Nice Euler Circuit Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu D ...
- UVALi 3263 That Nice Euler Circuit(几何)
That Nice Euler Circuit [题目链接]That Nice Euler Circuit [题目类型]几何 &题解: 蓝书P260 要用欧拉定理:V+F=E+2 V是顶点数; ...
随机推荐
- [HDOJ4609]3-idiots(FFT,计数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4609 题意:n个数,问取三个数可以构成三角形的组合数. FFT预处理出两个数的组合情况,然后枚举第三个 ...
- [python]实现单机版一行wordcount
用过spark,对wordcount这个演示程序记忆犹新,于是想试着实现一个简单的wordcount.又因为在学习函数式编程,希望可以把数据看成一个整体,在现有的函数上进行操作.于是就有了这一行代码. ...
- 关于oracle sql developer乱码的问题
写了一个sql查询,我擦居然乱码了 然后双击下这个框,居然又是中文: 有谁能够告诉我这是什么鬼
- [Effective Java]第三章 对所有对象都通用的方法
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- CUBRID学习笔记 39 net使用dataset 返回查询的数据
using CUBRID.Data.CUBRIDClient; namespace DataSetExample { class Program { static ...
- FJNU 1156 Fat Brother’s Gorehowl(胖哥的血吼)
FJNU 1156 Fat Brother’s Gorehowl(胖哥的血吼) Time Limit: 1000MS Memory Limit: 257792K [Description] [题目 ...
- webview页面和壳通信的库(精简版)
// PG精简版 (function() { var PG = { iosBridge: null, callbackId: 0, callbacks: [], commandQueue: [], c ...
- Java I/O NIO学习
给出一个学习的链接讲的很全.. http://ifeve.com/java-nio-all/ 上边的是中文翻译的这里是原地址:http://tutorials.jenkov.com/java-nio/ ...
- [置顶] 将项目从tomcat 迁移到JBoss
注:针对的是jboss5.0,其它版本没有测试过 ,主要参考了:http://www.diybl.com/course/3_program/java/javajs/20100719/460908.ht ...
- ubuntu安装jdk-6u45-linux-x64.bin___ZC_20160423
for : Android4.4源码编译 环境 : ubuntu12.04_desktop_amd64 1. 1.1.jdk-6u45-linux-x64.bin 放置于 /home 1.2.命令&q ...