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 题意: 给 ...
随机推荐
- android 获取activity 的name
String contextString = this.toString();String name = contextString.substring(contextString.lastIndex ...
- [转]VS2005 Debug时提示"没有找到MSVCR80D.dll"的解决办法
总结各种解决方法如下: 原因:(不知道在说啥)由于VS.net 2005 采用了一种新的DLL方案,搞成一个exe还要配有一个manifest文件(一般在嵌入文件里了,所以看不到,不过也可以不嵌入,这 ...
- windows azure中国 里面建立一个虚拟机,与虚拟机建立通信 里面部署IIS,外网访问
在windows azure中国 里面建立一个虚拟机,里面部署IIS,外网不能访问么? 外网访问的地址是给的那个DNS地址 ,比如我的是 DNS 名称 urbanairserver.cloudapp. ...
- Git搭建团队开发环境操作演练
模拟创建远程git仓库 1.首先创建如下目录结构: /Users/hujh/Desktop/GitTest2/GitServer/weibo weibo是我们要创建的项目 2.切换目录 $ cd /U ...
- getSingleResult 和 selectone
都是返回一个对象,如果找到一个以上的对象会报错,这个在登录验证和添加的时候可能会有点小用,因为登录和添加的时候都要判断是不是数据库有这个username,登录的时候希望有,添加的时候希望没有,但是两者 ...
- JavaScript 时间特效 显示当前时间
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- passivedns 安装指南
install passivedns on ubuntu Passive DNS对安全研究非常重要,因为它可以在前期帮助我们构建出目标的基础设施结构,并且可以得到以下三方面的答案:该域名曾经绑定过哪些 ...
- golang vim环境搭建
主要使用到的是golang的vim插件vim-go, 安装方法: (1)配置好自己的GOPATH和GOROOT: (2)在GOPATH目录下建立 src.pkg.main.bin四个目录: (3)安装 ...
- 技术分享:如何用Solr搭建大数据查询平台
0×00 开头照例扯淡 自从各种脱裤门事件开始层出不穷,在下就学乖了,各个地方的密码全都改成不一样的,重要帐号的密码定期更换,生怕被人社出祖宗十八代的我,甚至开始用起了假名字,我给自己起一新网名”兴才 ...
- __toString()与__call()
__toString()适用于直接输出类,用此方法,可以避免出错:__call()适用于使用类当中没有定义的函数(方法) <!DOCTYPE html> <html> < ...