POJ--2284--That Nice Euler Circuit【平面图欧拉公式】
链接: id=2284">http://poj.org/problem?id=2284
题意:一个自己主动绘图的机器在纸上(无限大)绘图,笔尖从不离开纸,有n个指令,每一个指令是一个坐标,由于笔尖不离开纸,所以相邻的坐标会连有一条直线,最后画笔再回到起始点。
所以这个图是一个连通图,而且画笔走过的路径是一个欧拉回路。
如今问题来了。这个图形将平面分成了几部分。
思路:题目说明确一些就是告诉你一些几何信息问平面被分成了几部分。能够用欧拉公式来做
欧拉公式:如果图的顶点个数为n,边数为m,区域数位r,则有 n - m + r = 2,前提必须是连通图
知道随意两个就能求第三个
#include<cstring>
#include<string>
#include<fstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cctype>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<stack>
#include<ctime>
#include<cstdlib>
#include<functional>
#include<cmath>
using namespace std;
#define PI acos(-1.0)
#define MAXN 90010
#define eps 1e-7
#define INF 0x3F3F3F3F //0x7FFFFFFF
#define LLINF 0x7FFFFFFFFFFFFFFF
#define seed 1313131
#define MOD 1000000007
#define ll long long
#define ull unsigned ll
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 struct Point{ //点
double x, y;
Point(double a = 0, double b = 0){
x = a;
y = b;
}
};
struct LineSegment{ //线段
Point s, e;
LineSegment(Point a, Point b){
s = a;
e = b;
}
};
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 fabs(p1.x - p2.x) < eps && fabs(p1.y - p2.y) < eps;
}
bool Online(LineSegment l, Point p){ //推断点是否在线段上
return fabs((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 MakeLine(Point p1, Point p2){ //将线段延长为直线
Line l;
if(p2.y > p1.y){
l.a = p2.y - p1.y;
l.b = p1.x - p2.x;
l.c = p1.y * p2.x - p1.x * p2.y;
}
else{
l.a = p1.y - p2.y;
l.b = p2.x - p1.x;
l.c = p1.x * p2.y - p1.y * p2.x;
}
return l; //返回直线
}
bool LineIntersect(Line l1, Line l2, Point &p){ //推断直线是否相交。并求出交点p
double d = l1.a * l2.b - l2.a * l1.b;
if(fabs(d) < eps) return false;
//求交点
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 LineSegmentIntersect(LineSegment l1, LineSegment l2, Point &p){ //推断线段是否相交
Line a, b;
a = MakeLine(l1.s, l1.e), b = MakeLine(l2.s, l2.e); //将线段延长为直线
if(LineIntersect(a, b, p)) //假设直线相交
return Online(l1, p) && Online(l2, p); //推断直线交点是否在线段上,是则线段相交
else
return false;
} bool cmp(Point a, Point b){
if(fabs(a.x - b.x) < eps) return a.y < b.y;
else return a.x < b.x;
}
Point p[MAXN], Intersection[MAXN];
int N, m, n;
int main(){
int i, j, cas = 1;
while(scanf("%d", &N), N){
m = n = 0;
for(i = 0; i < N; i++){
scanf("%lf%lf", &p[i].x, &p[i].y);
}
for(i = 0; i < N; i++){
for(j = 0; j < N; j++){
LineSegment l1(p[i], p[(i + 1) % N]), l2(p[j], p[(j + 1) % N]);
Point p;
if(LineSegmentIntersect(l1, l2, p))
Intersection[n++] = p; //记录交点
}
}
sort(Intersection, Intersection + n, cmp);
n = unique(Intersection, Intersection + n) - Intersection;
for(i = 0; i < n; i++){
for(j = 0; j < N; j++){
LineSegment t(p[j], p[(j + 1) % N]);
if(Online(t, Intersection[i]) && !(t.s == Intersection[i])) //若有交点落在边上,则该边分裂成两条边
m++;
}
}
printf("Case %d: There are %d pieces.\n", cas++, 2 - n + m);
}
return 0;
}
POJ--2284--That Nice Euler Circuit【平面图欧拉公式】的更多相关文章
- 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 ...
- POJ 2284 That Nice Euler Circuit (LA 3263 HDU 1665)
http://poj.org/problem?id=2284 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&a ...
- POJ2284 That Nice Euler Circuit (欧拉公式)(计算几何 线段相交问题)
That Nice Euler Circuit Time Limit: 3000MS M ...
- pku 2284 That Nice Euler Circuit
题意: 给你n个点第n个点保证与第0个点相交,然后求这n个点组成的图形可以把整个平面分成几个面 思路: 这里的解题关键是知道关于多面体的欧拉定理 多面体: 设v为顶点数,e为棱数,f是面数,则v-e+ ...
- poj2284 That Nice Euler Circuit(欧拉公式)
题目链接:poj2284 That Nice Euler Circuit 欧拉公式:如果G是一个阶为n,边数为m且含有r个区域的连通平面图,则有恒等式:n-m+r=2. 欧拉公式的推广: 对于具有k( ...
- UVALive - 3263 That Nice Euler Circuit (几何)
UVALive - 3263 That Nice Euler Circuit (几何) ACM 题目地址: UVALive - 3263 That Nice Euler Circuit 题意: 给 ...
- That Nice Euler Circuit(LA3263+几何)
That Nice Euler Circuit Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu D ...
- UVa 10735 (混合图的欧拉回路) Euler Circuit
题意: 给出一个图,有的边是有向边,有的是无向边.试找出一条欧拉回路. 分析: 按照往常的思维,遇到混合图,我们一般会把无向边拆成两条方向相反的有向边. 但是在这里却行不通了,因为拆成两条有向边的话, ...
- UVA 10735 Euler Circuit 混合图的欧拉回路(最大流,fluery算法)
题意:给一个图,图中有部分是向边,部分是无向边,要求判断是否存在欧拉回路,若存在,输出路径. 分析:欧拉回路的定义是,从某个点出发,每条边经过一次之后恰好回到出发点. 无向边同样只能走一次,只是不限制 ...
随机推荐
- appium之android_uiautomator定位进阶版
前言 上一篇介绍uiautomator的定位方式都是类似这种'new UiSelector().xxx("xxx")',看起非常长,我也记不住,这很不python.于是本篇优化了定 ...
- BNUOJ 3580 Oulipo
Oulipo Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 3 ...
- ImportError: No module named ‘MySQLdb'
1.APP未注册 INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contentty ...
- 【JavaScript 1—基础知识点】:宏观概述
导读:JavaScript是一门新的(也可以说是旧的或者半新语言),里面有很多的知识点都能和已有的知识产生共鸣.但是,虽然简单,相同点也有很多,也有不同点.我脑袋也不好使,所以对于我来说,还是有必要再 ...
- 九度oj 题目1250:矩阵变换
题目描述: 对于一个整数矩阵,存在一种运算,对矩阵中任意元素加一时,需要其相邻(上下左右)某一个元素也加一, 现给出一正数矩阵,判断其是否能够由一个全零矩阵经过上述运算得到. 输入: 输出: 如果可以 ...
- POJ 3581 Sequence ——后缀数组 最小表示法
[题目分析] 一见到题目,就有了一个显而易见obviously的想法.只需要每次找到倒过来最小的那一个字符串翻转就可以了. 然而事情并不是这样的,比如说505023这样一个字符串,如果翻转了成为320 ...
- BZOJ 1008: [HNOI2008]越狱【组合】
很少有的思路秒解.题意可以描述成对长度为n的格子有m种染色方案,问存在相邻两个格子同色的方案数,正难则反易,考虑问题的背面任意两个相邻的格子都不同色,第一个格子可以涂任意一种颜色m种可能,剩下的n-1 ...
- 算法复习——LCA模板(POJ1330)
题目: Description A rooted tree is a well-known data structure in computer science and engineering. An ...
- 刷题总结——保留道路(ssoj)
题目: 题目背景 161114-练习-DAY1-AHSDFZ T3 题目描述 很久很久以前有一个国家,这个国家有 N 个城市,城市由 1,2,3,…,,N 标号,城市间有 M 条双向道路,每条道路都有 ...
- Modular Production Line
Modular Production Line 时空限制: 1000ms /65536K An automobile factory has a car production line. Now ...