UVALive 3263 That Nice Euler Circuit calculate geometric Euler's theorem, uvalive3263

Source: Internet
Author: User

UVALive 3263 That Nice Euler Circuit calculate geometric Euler's theorem, uvalive3263


Euler's theorem: P + F-E = 2


That Nice Euler Circuit
Time Limit:3000 MS   Memory Limit:Unknown   64bit IO Format:% Lld & % llu

Submit Status

Description

Little Joey got Ted 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 cocould 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 penpencil touching the paper, and a control center issuing a sequence of instructions. the paper can be viewed as the infinite two-dimenstmplane; that means you do not need to worry about if the penpencil will ever go off the boundary.

In the beginning, the Euler machine will issue an instruction of the form (X0,Y0) which moves the penpencil to some starting position (X0,Y0). Each subsequent instruction is also of the form (X',Y'), Which means to move the penpencil 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 penpencil 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 penpencil 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. parse case starts with a line containing an integerN4, which is the number of instructions in the test case. The followingNPairs 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 whenNIs 0.

Output

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


CaseX: There areWPieces.,


WhereXIs the serial number starting from 1.


Note:The figures below has strate the two sample input cases.

Sample Input

50 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.

Source

Submit Status



# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <cmath> # include <vector> using namespace std; const double eps = 1e-8; int dcmp (double x) {if (fabs (x) <eps) return 0; return (x <0 )? -1:1;} struct Point {double x, y; Point (double _ x = 0, double _ y = 0): x (_ x), y (_ y) {};}; Point operator + (Point A, Point B) {return Point (. x + B. x,. y + B. y);} Point operator-(Point A, Point B) {return Point (. x-B.x,. y-B.y);} Point operator * (Point A, double p) {return Point (. x * p,. y * p);} Point operator/(Point A, double p) {return Point (. x/p,. y/p);} bool operator <(const Point & a, const Point & B) {return. x <B. x | (. x = B. X &. y <B. y);} bool operator = (const Point & a, const Point & B) {return dcmp (. x-b.x) = 0 & dcmp (. y-b.y) = 0;} double Dot (Point A, Point B) {return. x * B. x +. y * B. y;} double Length (Point A) {return sqrt (Dot (A, A);} double Angle (Point A, Point B) {return acos (Dot (, b)/Length (A)/Length (B);} double Angle (Point v) {return atan2 (v. y, v. x);} double Cross (Point A, Point B) {return. x * B. y-A.y * B. x;}/** Cross P * Q> 0 P clockwise in Q Direction P * Q <0 P counterclockwise direction P * Q = 0 PQ collinearity */Point Horunit (Point x) {return x/Length (x );} /// unit vector Point Verunit (Point x) {return Point (-x. y, x. x)/Length (x);} // unit method vector Point Rotate (Point A, double rad) // counter-clockwise rotation {return Point (. x * cos (rad)-. y * sin (rad),. x * sin (rad) +. y * cos (rad);} double Area2 (const Point A, const Point B, const Point C) {return Cross (B-A, C-A );} /// over two point p1, p2 linear General Equation ax + by + c = 0 (x2-x1) (y-y1) = (y2-y1) (x-x1) Void getLineGeneralEquation (const Point & p1, const Point & p2, double & a, double & B, double & c) {a = p2.y-p1.y; B = p1.x-p2.x; c =-a * p1.x-B * p1.y;} // P + t * v Q + w * t focus Point GetLineIntersection (Point P, Point v, Point Q, point w) {Point u = P-Q; double t = Cross (w, u)/Cross (v, w); return P + v * t ;} // distance from A Point to A straight line double DistanceToLine (Point P, Point A, Point B) {Point v1 = B-A, v2 = P-A; return fabs (Cross (v1, v2 )) /Length (v1 );}/ // Distance from A Point to A line segment double DistanceToSegment (Point P, Point A, Point B) {if (A = B) return Length (P-A); Point v1 = B-A, v2 = P-A, v3 = P-B; if (dcmp (Dot (v1, v2) <0) return Length (v2); else if (dcmp (Dot (v1, v3)> 0) return Length (v3); else return fabs (Cross (v1, v2)/Length (v1);} // Point to linear projection Point GetLineProjection (Point P, Point, point B) {Point v = B-A; return A + v * (Dot (v, P-A)/Dot (v, v ));} /// determine the canonicalized bool SegmentProperIntersection (Point A1, Point a2, Point b1, Point b2) {double c1 = Cross (a2-a1, b1-a1), c2 = Cross (a2-a1, b2-a1); double c3 = Cross (b2-b1, a1-b1), c4 = Cross (b2-b1, a2-b1); return dcmp (c1) * dcmp (c2) <0 & dcmp (c3) * dcmp (c4) <0 ;} /// whether a Point is on the straight-line endpoint bool OnSegment (Point p, Point a1, Point a2) {return dcmp (Cross (a1-p, a2-p )) = 0 & dcmp (Dot (a1-p, a2-p) <0 ;}// polygon directed Area double PolygonArea (Point * p, int n) {double area = 0; for (int I = 1; I <n-1; I ++) area + = Cross (p [I] -P [0], p [I + 1]-p [0]); return area/2;} int n; Point pt [1000]; vector <Point> vp; int main () {int cas = 1; while (scanf ("% d", & n )! = EOF & n) {vp. clear (); for (int I = 0; I <n; I ++) {scanf ("% lf", & pt [I]. x, & pt [I]. y); vp. push_back (pt [I]);} n --; for (int I = 0; I <n; I ++) {for (int j = I + 1; j <n; j ++) {if (SegmentProperIntersection (pt [I], pt [I + 1], pt [j], pt [j + 1]) {vp. push_back (GetLineIntersection (pt [I], pt [I + 1]-pt [I], pt [j], pt [j + 1]-pt [j]); }}} sort (vp. begin (), vp. end (); int c = unique (vp. begin (), vp. end ()-vp. begin (); int e = n; int cc = 0; for (vecto R <Point>: iterator it = vp. begin (); cc <c & it! = Vp. end (); cc ++, it ++) {for (int I = 0; I <n; I ++) {if (OnSegment (* it, pt [I], pt [I + 1]) e ++;} printf ("Case % d: There are % d pieces. \ n ", cas ++, e + 2-c);} return 0 ;}





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.