That Nice Euler Circuit
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 1977   Accepted: 626

Description

Little Joey invented a scrabble machine that he called Euler, after the great mathematician. In his primary school Joey heard about the nice story of how Euler started the study about graphs. The problem in that story was - let me remind you - to draw a graph on a paper without lifting your pen, and finally return to the original position. Euler proved that you could do this if and only if the (planar) graph you created has the following two properties: (1) The graph is connected; and (2) Every vertex in the graph has even degree.

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

There
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

For each test case there will be one output line in the format

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 (欧拉公式)(计算几何 线段相交问题)的更多相关文章

  1. poj2284 That Nice Euler Circuit(欧拉公式)

    题目链接:poj2284 That Nice Euler Circuit 欧拉公式:如果G是一个阶为n,边数为m且含有r个区域的连通平面图,则有恒等式:n-m+r=2. 欧拉公式的推广: 对于具有k( ...

  2. ZOJ1648 Circuit Board(线段相交)

    裸的判断线段相交

  3. UVALive 3263: That Nice Euler Circuit (计算几何)

    题目链接 lrj训练指南 P260 //==================================================================== // 此题只需要考虑线 ...

  4. POJ 3347 Kadj Squares (计算几何+线段相交)

    题意:从左至右给你n个正方形的边长,接着这些正方形都按照旋转45度以一角为底放置坐标轴上,最左边的正方形左端点抵住y轴,后面的正方形依次紧贴前面所有正方形放置,问从上方向下看去,有哪些正方形是可以被看 ...

  5. poj1410计算几何线段相交

    You are to write a program that has to decide whether a given line segment intersects a given rectan ...

  6. zoj 1010 Area【线段相交问题】

    链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1010 http://acm.hust.edu.cn/vjudge/ ...

  7. POJ--2284--That Nice Euler Circuit【平面图欧拉公式】

    链接:id=2284">http://poj.org/problem?id=2284 题意:一个自己主动绘图的机器在纸上(无限大)绘图,笔尖从不离开纸,有n个指令,每一个指令是一个坐标 ...

  8. That Nice Euler Circuit UVALive - 3263 || 欧拉公式

    欧拉定理: 简单多面体的顶点数V.棱数E及面数F间有关系有著名的欧拉公式:V-E+F=2. 设G为任意的连通的平面图,则v-e+f=2,v是G的顶点数,e是G的边数,f是G的面数.(引) 证明(?) ...

  9. UVALive - 3263 That Nice Euler Circuit (几何)

    UVALive - 3263 That Nice Euler Circuit (几何) ACM 题目地址:  UVALive - 3263 That Nice Euler Circuit 题意:  给 ...

随机推荐

  1. MySQL数据库远程连接

    12.00 MySQL数据库远程连接 参考: http://www.jb51.net/article/24508.htm http://www.linuxdiyf.com/viewarticle.ph ...

  2. 千寻浏览器 1.0 Beta 1(524)(2014年5月27日)

    千寻浏览器--又一款新生浏览器今天进入各位浏览迷的视野.千寻浏览器基于IE内核,据传是由百度浏览器的上海团队操刀,在功能定位上,与目前的QQ浏览器有些相似. 千寻来自官方的解释:寻,追寻,探索,又是古 ...

  3. 记录一些容易忘记的属性 -- UIView

    一个视图原来添加在某个父视图上,然后再将它添加到另外的一个视图上,这个视图会从原来的某个父视图中移除,添加到新的视图上. 子视图对象指针存在父视图的subviews数组中,说明,一个视图可以有多个子视 ...

  4. AS的快捷键

    Ctrl+Shift+Alt+N 查找类中的方法或变量 Ctrl+P 方法参数提示 Alt+Insert 生成代码(如get,set方法,构造函数等) 删除导入多余的包Ctrl+Alt+o 提取局部变 ...

  5. ThinkPHP中Session用法详解

    在ThinkPHP封装了Session类,用户可以直接使用,常用的方法有: Session::set(name, value):注册 session . Session::is_set(name):检 ...

  6. TPLink 备份文件bin文件解析[续]

    Most routers allow to save and restore configuration from files. This is cool because you can edit t ...

  7. HttpWebRequest与HttpWebResponse使用例子(转)

    转自:http://www.jb51.net/article/28401.htm 在每个系统出写入报告错误代码(找个合理的理由,比如系统免费升级) -> 自家服务器接收并处理错误报告 -> ...

  8. RFIDler:一款定义RFID的读、写、仿真器的开源软件

    很多类似于RFID这样的技术看起来都很神秘,实际上他是依赖于很多物理学原理的,比如”电磁感应原理”.是的,这些现象产生的各种信号足以令人发狂,看完这些模拟模拟信号后,我忽然发现二进制信息多么干净美丽. ...

  9. HDU 1693 Eat the Trees

    第一道(可能也是最后一道)插头dp.... 总算是领略了它的魅力... #include<iostream> #include<cstdio> #include<cstr ...

  10. ctrl+shift+del 清理火狐缓存,解决页面显示错乱问题

    ctrl+shift+del 清理火狐缓存,解决页面显示错乱问题