La_3263_that_nice_euler_circuits_ (Euler theorem + computational geometry)

Source: Internet
Author: User

Describe

Https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=15&page=show _problem&problem=1264

All vertices of a stroke are given, and the stroke is divided into several regions (including priority areas and infinite regions).

3263-that Nice Euler Circuit

3263
That nice Euler circuit
Little Joey invented a Scrabble machine this he called Euler, after the great mathematician. In his
Primary school Joey heard about the nice story of what Euler started the study about graphs. The
Problem in so 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 could does this if and only if the
(planar) Graph you created have the following the properties: (1) the graph is connected; and (2) every
Vertex in the graph has even degree.
Joey's Euler machine works exactly. 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's means you don't need to worry about if the pencil would ever go off the boundary.
In the beginning, the Euler machine would issue an instruction of the form (X0, Y 0) which moves the
Pencil to some starting position (X0, Y 0). 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 are different from the previous position
For each instruction. At last, the Euler machine would always issue a instruction that move the pencil
Back to the starting position (X0, Y 0). In addition, the Euler machine would definitely not draw any
Lines that overlay other lines already drawn. However, the lines may intersect.
After all the instructions is issued, there'll be a nice picture on Joey's paper. You see, since the
Pencil is never lifted from the paper and the picture can be viewed as an Euler circuit.
Your job is to count how many pieces (connected areas) was created on the paper by those lines
Drawn by Euler.
Input
There is no more than 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. Assume there is no more than-instructions in
Each test case, and all the integer coordinates is in the range (-300, 300). The input is terminated
When N is 0.
Output
For each test case there'll be one output line in the format
Case X:there is w pieces.,
where x is the serial number starting from 1.
Note:the figures below illustrate the and the 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
0ACM-ICPC Live archive:3263–that Nice Euler circuit
Sample Output
Case 1:there is 2 pieces.
Case 2:there is 5 pieces.
2/2

Analysis

Euler's theorem: the number of vertices of a plane graph V, the number of sides E, the number of faces F satisfies v+f-e=2.

In this way, only the number of points and edges is required.

The points are divided into two parts: 1. There are already. 2. Intersection (canonical intersection) obtained.

The side can also be divided into two parts: 1. There are already. 2. Each result in a new point, the number of edges +1, because of the specification intersection.

Attention:

1. There may be a three-wire common point, at this point the intersection is calculated two times, so to go to heavy, you can use the unique function (the array must be ordered, if starting from a[1], the return value to-(a+1)).

2. Input is n+1! He entered the starting point again at the end! It's been a long time.

1#include <bits/stdc++.h>2 using namespacestd;3 4 Const intmaxn= -+Ten;5 Const Doubleeps=1e-8;6 7 intn,kase;8 structpoint{9     Doublex, y;TenPoint (Doublex=0,Doubley=0): X (x), Y (y) {} One}p[maxn],v[maxn*MAXN]; A typedef point Vector; - intDCMP (Doublex) { -     if(Fabs (x) <eps)return 0; the     returnx<0?-1:1; - } -Vectoroperator+ (Vector A,vector b) {returnVector (a.x+b.x,a.y+b.y); } -Vectoroperator-(Vector A,vector b) {returnVector (a.x-b.x,a.y-b.y); } +Vectoroperator* (Vector A,DoubleP) {returnVector (a.x*p,a.y*p); } -Vectoroperator/(Vector A,DoubleP) {returnVector (a.x/p,a.y/p); } + BOOL operator< (ConstPoint &a,ConstPoint &b) {returna.x<b.x| | (a.x==b.x&&a.y<b.y); } A BOOL operator== (ConstPoint &a,ConstPoint &b) {returnDCMP (a.x-b.x) = =0&AMP;&AMP;DCMP (a.y-b.y) = =0; } at DoubleDot (Vector A,vector b) {returna.x*b.x+a.y*b.y;} - DoubleCross (Vector A,vector b) {returna.x*b.y-a.y*b.x;} - BOOLsegment_proper_intersection (Point a,point b,point c,point d) { -     DoubleC1=cross (b-a,c-a), C2=cross (b-a,d-a), -C3=cross (d-c,a-c), C4=cross (d-c,b-c); -         return(DCMP (C1) ^dcmp (C2)) ==-2&& (DCMP (C3) ^dcmp (C4)) ==-2; in } - Point get_line_intersection (Point p,vector v,point q,vector W) { toVector u=p-Q; +     DoubleT=cross (w,u)/Cross (v,w); -     returnp+v*T; the } * BOOLOn_segment (Point p,point a,point b) {returnDCMP (Cross (a-p,b-p)) = =0&AMP;&AMP;DCMP (dot (a-p,b-p) <0); } $ Panax Notoginseng voidsolve () { -      for(intI=1; i<=n;i++){ thescanf"%LF%LF",&p[i].x,&p[i].y); +v[i]=P[i]; A     } then--;//the input is a n+1 dot . +     intC=n,e=N; -      for(intI=1; i<=n;i++) $          for(intj=i+1; j<=n;j++){ $             if(Segment_proper_intersection (p[i],p[i+1],p[j],p[j+1])) -V[++c]=get_line_intersection (p[i],p[i+1]-p[i],p[j],p[j+1]-p[j]);} -Sort (v+1, v+c+1); theC=unique (v+1, v+c+1)-(v+1);//go heavy, pay attention to minus the time (v+1) -      for(intI=1; i<=c;i++)Wuyi          for(intj=1; j<=n;j++) the             if(On_segment (v[i],p[j],p[j+1])) e++; -printf"Case %d:there is%d pieces.\n", ++kase,e+2-c);//Euler's theorem Wu } - intMain () { About      while(SCANF ("%d", &n) &&N) solve (); $     return 0; -}
View Code

La_3263_that_nice_euler_circuits_ (Euler theorem + computational geometry)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.