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. VMware-workstation-full-10.0.1-1379776 CN

    从V10版本开始,VMware Workstation 官方自带简体中文了,以后大家不需要汉化啦! 今天,VMware Workstation 10.0.1正式发布,版本号为Build 1379776 ...

  2. SharePoint开发 - 自定义导航菜单(一)菜单声明与配置

    博客地址 http://blog.csdn.net/foxdave 本篇描述自定义sharepoint菜单的一种方式,自定义菜单适用于一些门户等需求的网站 自定义的菜单有自己的数据源,可以是数据表,可 ...

  3. 一篇很好介绍stringBuffer和StringBuilder的区别--来自百度

    ava.lang.StringBuffer线程安全的可变字符序列.一个类似于 String 的字符串缓冲区,但不能修改.虽然在任意时间点上它都包含某种特定的字符序列,但通过某些方法调用可以改变该序列的 ...

  4. Xcode如何查看内存中的数据

    在  debug 模式下如何在断点处,查看字符指针变量内存中的值,像vs2008的调试工具一样的内存查看器,现在只能查看第一个内存中的值可以在输出窗口采用gdb命令:x /nfu <addr&g ...

  5. [转] linux中巧用ctrl-z后台运行程序

    背景: 最近在执行一些长时间程序的时候,老是一不小心忘了输入‘&’ , 结果终端就卡在那里了,很是郁闷 以前总是再新开一个终端. 今天翻看<鸟哥的linux私房菜>的时候,发现介绍 ...

  6. Win8远程[你的凭据不工作]的解决办法

    用户名前加你的计算机名称就可以了....win8也会有这样的问题,有点匪夷所思了......

  7. Python文本(字面值)

    Python中的文本是一些内置类型的常量表示方法. 字符串和字节 字符串是一系列的字符序列,Python中用单引号(''),双引号(""),或者三个单引号(''' ''')三个双引 ...

  8. 【LeetCode OJ】Word Break

    Problem link: http://oj.leetcode.com/problems/word-break/ We solve this problem using Dynamic Progra ...

  9. pscp详解

    pscp详解 在linux中,我们常用scp命令传输文件: 如以下实例,我们想把当前服务器文件abc.sql传输到192.168.1.1服务器上,我们可以执行以下命令: scp /home/perso ...

  10. WPF入门教程系列(一) 创建你的第一个WPF项目

    WPF入门教程系列(一) 创建你的第一个WPF项目 WPF基础知识 快速学习绝不是从零学起的,良好的基础是快速入手的关键,下面先为大家摞列以下自己总结的学习WPF的几点基础知识: 1) C#基础语法知 ...