POJ2284 That Nice Euler Circuit (欧拉公式)(计算几何 线段相交问题)
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 1977 | Accepted: 626 |
Description
Joey's Euler machine works exactly like this. The device consists of
a pencil touching the paper, and a control center issuing a sequence of
instructions. The paper can be viewed as the infinite two-dimensional
plane; that means you do not need to worry about if the pencil will ever
go off the boundary.
In the beginning, the Euler machine will issue an instruction of the
form (X0, Y0) which moves the pencil to some starting position (X0,
Y0). Each subsequent instruction is also of the form (X', Y'), which
means to move the pencil from the previous position to the new position
(X', Y'), thus draw a line segment on the paper. You can be sure that
the new position is different from the previous position for each
instruction. At last, the Euler machine will always issue an instruction
that move the pencil back to the starting position (X0, Y0). In
addition, the Euler machine will definitely not draw any lines that
overlay other lines already drawn. However, the lines may intersect.
After all the instructions are issued, there will be a nice picture
on Joey's paper. You see, since the pencil is never lifted from the
paper, the picture can be viewed as an Euler circuit.
Your job is to count how many pieces (connected areas) are created on the paper by those lines drawn by Euler.
Input
are no more than 25 test cases. Ease case starts with a line containing
an integer N >= 4, which is the number of instructions in the test
case. The following N pairs of integers give the instructions and appear
on a single line separated by single spaces. The first pair is the
first instruction that gives the coordinates of the starting position.
You may assume there are no more than 300 instructions in each test
case, and all the integer coordinates are in the range (-300, 300). The
input is terminated when N is 0.
Output
Case x: There are w pieces.,
where x is the serial number starting from 1.
Note: The figures below illustrate the two sample input cases.
Sample Input
5
0 0 0 1 1 1 1 0 0 0
7
1 1 1 5 2 1 2 5 5 1 3 5 1 1
0
Sample Output
Case 1: There are 2 pieces.
Case 2: There are 5 pieces.
欧拉公式:对任意平面图,顶点数n,边数m且含有r个区域,则有 n-m+r=2.这题最难得还是判断两线段是否相交并求出相交点。
#include<cstdio>
#include<cstring>
#include<stdlib.h>
#define inf 0xffffff
#include<iostream>
#include<cmath>
#define NUM 22
#include <algorithm>
using namespace std; const double eps=1e-;
struct point {
double x,y;
point(double a=,double b=) {
x=a;
y=b;
}
};
bool operator< (point a, point b) {
return a.x<b.x||a.x==b.x&&a.y<b.y;
}
bool operator == (point a,point b) {
return abs(a.x-b.x)<eps&&abs(a.y-b.y)<eps;
}
struct Lineseg {
point s,e;
Lineseg(point a, point b) {
s=a;
e=b;
}
};
struct Line {
double a,b,c;
};
bool online(Lineseg L,point p) { //判断p是否在线段L上
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 Makeline(Lineseg tmp) { //线段L变成L
Line L;
int x1=tmp.s.x;
int y1=tmp.s.y;
int x2=tmp.e.x;
int y2=tmp.e.y;
if(y2-y1>) {
L.a=(y2-y1);
L.b=(x1-x2);
L.c=(x2*y1-x1*y2);
} else {
L.a=(y1-y2);
L.b=(x2-x1);
L.c=(x1*y2-x2*y1);
}
return L;
}
bool Lineinter(Line x,Line y,point &q) { //直线X,Y相交于点q
double d=x.a*y.b-y.a*x.b;
if(abs(d)<eps)
return false;
q.x=(y.c*x.b-x.c*y.b)/d;
q.y=(y.a*x.c-x.a*y.c)/d;
return ;
} bool Lineseginter(Lineseg aa,Lineseg bb,point &q) { //线段aa,bb如果相交则返回交点q
Line a,b;
a=Makeline(aa);
b=Makeline(bb);
if(Lineinter(a,b,q))
return online(aa,q)&&online(bb,q);
else
return false;
}
bool cmp(point a ,point b) {
if(a.x==b.x)
return a.y<b.y;
else
return a.x<b.x;
}
point p[];
point inter[];
int N;
int main() {
int m,n;
int T=;
while(scanf("%d",&N),N) {
m=n=;
int cnt=;
for(int i=; i<N; i++)
scanf("%lf %lf",&p[i].x,&p[i].y);
for(int i=; i<N; i++) {
for(int j=; j<N; j++) {
Lineseg L1(p[i],p[(i+)%N]),L2(p[j],p[(j+)%N]);
point q;
if(Lineseginter(L1,L2,q))
inter[cnt++]=q;
}
}
sort(inter,inter+cnt,cmp);
n=unique(inter,inter+cnt)-inter;//去重复的点
for(int i=; i<n; i++) {
for(int j=; j<N; j++) {
Lineseg t(p[j],p[(j+)%N]);
if(online(t,inter[i])&&!(t.s==inter[i]))m++;
}
}
T++;
printf("Case %d: There are %d pieces.\n",T,m+-n);//欧拉定理
}
return ;
}
POJ2284 That Nice Euler Circuit (欧拉公式)(计算几何 线段相交问题)的更多相关文章
- poj2284 That Nice Euler Circuit(欧拉公式)
题目链接:poj2284 That Nice Euler Circuit 欧拉公式:如果G是一个阶为n,边数为m且含有r个区域的连通平面图,则有恒等式:n-m+r=2. 欧拉公式的推广: 对于具有k( ...
- ZOJ1648 Circuit Board(线段相交)
裸的判断线段相交
- UVALive 3263: That Nice Euler Circuit (计算几何)
题目链接 lrj训练指南 P260 //==================================================================== // 此题只需要考虑线 ...
- POJ 3347 Kadj Squares (计算几何+线段相交)
题意:从左至右给你n个正方形的边长,接着这些正方形都按照旋转45度以一角为底放置坐标轴上,最左边的正方形左端点抵住y轴,后面的正方形依次紧贴前面所有正方形放置,问从上方向下看去,有哪些正方形是可以被看 ...
- poj1410计算几何线段相交
You are to write a program that has to decide whether a given line segment intersects a given rectan ...
- zoj 1010 Area【线段相交问题】
链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1010 http://acm.hust.edu.cn/vjudge/ ...
- POJ--2284--That Nice Euler Circuit【平面图欧拉公式】
链接:id=2284">http://poj.org/problem?id=2284 题意:一个自己主动绘图的机器在纸上(无限大)绘图,笔尖从不离开纸,有n个指令,每一个指令是一个坐标 ...
- That Nice Euler Circuit UVALive - 3263 || 欧拉公式
欧拉定理: 简单多面体的顶点数V.棱数E及面数F间有关系有著名的欧拉公式:V-E+F=2. 设G为任意的连通的平面图,则v-e+f=2,v是G的顶点数,e是G的边数,f是G的面数.(引) 证明(?) ...
- UVALive - 3263 That Nice Euler Circuit (几何)
UVALive - 3263 That Nice Euler Circuit (几何) ACM 题目地址: UVALive - 3263 That Nice Euler Circuit 题意: 给 ...
随机推荐
- POJ 1845 求a^b的约数和
题目大意就是给定a和b,求a^b的约数和 f(n) = sigma(d) [d|n] 这个学过莫比乌斯反演之后很容易看出这是一个积性函数 那么f(a*b) = f(a)*f(b) (gcd(a,b) ...
- Problem A 栈
Description You are given a string consisting of parentheses () and []. A string of this type is s ...
- hdu 1035 (usage of sentinel, proper utilization of switch and goto to make code neat) 分类: hdoj 2015-06-16 12:33 28人阅读 评论(0) 收藏
as Scott Meyers said in his book Effective STL, "My advice on choosing among the sorting algori ...
- tic/toc/cputime测试时间
cputime测试代码运行时间可能不及tic/toc准确是众所周知的事情.本文并非旧话重提,而是期望起到抛砖引玉的效果,从而找到cputime与tic/toc内在的区别.望不吝赐教! 用tic/toc ...
- [流媒体]live555简介(转)
live555简介 Live555 是一个为流媒体提供解决方案的跨平台的C++开源项目,它实现了对标准流媒体传输协议如RTP/RTCP.RTSP.SIP等的支持.Live555实现 了对多种音视频编码 ...
- javascript预解析和作用域
JavaScript解析过程分为两个阶段: 一是:编译阶段.就是JavaScrip预解析阶段,在这个阶段JavaScript解析器将完成把JavaScript脚本代码转换到字节码; 二是:执行阶段.在 ...
- NCBI原始数据下载by Aspera Connect
主要参考这篇文章: http://mp.weixin.qq.com/s?__biz=MzA5NjU5NjQ4MA==&mid=2651154488&idx=1&sn=e693a ...
- SVG 2D入门1 - SVG综述
位图与矢量图 以前,浏览器中显示的图形,例如jpeg.gif等,都是位图,这些图像格式是基于光栅的.在光栅图像中,图像文件定义了图像中每个像素的颜色值.浏览器需要读取这些值并做出相应行动.这种图像的再 ...
- 2016 - 1 - 19NSOpertation的依赖关系和监听
一:NSOperation的依赖: 1.概念:队列中的A操作需要等其他B操作或者某些操作执行完毕后才执行,就叫做A依赖与B或者A依赖于其他某些操作. 2.注意点:不能循环依赖,否则卡死.如: [op2 ...
- 黑马程序员——【Java基础】——Java语法基础
---------- android培训.java培训.期待与您交流! ---------- 一.关键字 1.定义和特点 定义:被Java语言赋予了特殊含义的单词. 特点:关键字中所有的字母都为小写. ...