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 题意: 给 ...
随机推荐
- Ohlàlà
Chap 1数数字 un 1 deux 2 trois 3 quatre 4 cinq 5 six 6 sept 7 huit 8 neuf 9 dix 10 Chap 2 讲地名 Paris 巴 ...
- SSH(1)
假定服务器ip为192.168.1.139,ssh服务的端口号为22,服务器上有个用户为pi,两边都是ubuntu 一,Init *安装 如果是想用ssh从本机登陆别的机器,只需要安装openssh- ...
- swift语言之多线程操作和操作队列(上)———坚持51天吃掉大象
欢迎有兴趣的朋友,参与我的美女同事发起的活动<51天吃掉大象>,该美女真的很疯狂,希望和大家一起坚持51天做一件事情,我加入这个队伍,希望坚持51天每天写一篇技术文章.关注她的微信公众号: ...
- Javascript基础--类与对象(五)
js面向(基于)对象编程1.澄清概念 1.1 js中基于对象 == js 面向对象 1.2 js中没有类class,但是它取了一个新的名字,交原型对象,因此 类 = 原型对象. 2.为什么需要对象? ...
- PHP同一个IP绑定多个域名(六)
原理图 一个ip绑定如何绑定多个域名? 解决方案: A.方案一:端口号来区别不同的站点 1.绑定一个网站 1.1先开发好自己的网站 d:/ApacheProject/myanimal 1.2 配置我们 ...
- 找不到库文件地址,修改修改方法framework
直接双击地址行修改
- jvm之xms、xmx等参数分析
注:本文摘自http://www.cnblogs.com/mingforyou/archive/2012/03/03/2378143.html ,感谢原作者 1.参数的含义-vmargs -Xms12 ...
- Chapter 3: Connector(连接器)
一.概述 Tomcat或者称之为Catalina(开发名称),可以简化为两个主要的模块,如下图: 多个Connector关联一个Container.之所以需要多个Connector,是为了处理多种协议 ...
- 深入分析:Android中app之间的交互(二,使用ComponentName)
在前一篇相关主题的博文中我们了解了如何使用Action来启动当前应用之外的Activity处理我们的业务逻辑,在本篇笔记中我在简单介绍一下使用ComponentName来与当前应用之外的应用进行交互. ...
- Python OpenCV —— Border
关于border的部分,边缘处理. # -*- coding: utf-8 -*- """ Created on Wed Sep 28 00:58:51 2016 @au ...