La 3236 that nice Euler Circuit (Euler's theorem)

Source: Internet
Author: User

That nice Euler Circuit

Timelimit: 3.000 seconds


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.

There is a stroke on the plane that contains N endpoints. The Nth endpoint always overlaps with the first endpoint, so a closed curve is formed. The line segments that make up a stroke can be intersection, but do not partially overlap. Determine the part of the line segment that divides the plane into (including the Closed Area and the infinite area ).

Analysis: it is difficult and error-prone to identify all regions directly. However, the Euler's theorem can be used to convert the problem and make the solution easier.

Euler's theorem: Let the number of vertices, number of edges, and number of faces in the plot be V, E, F, respectively, then V + F-E = 2.

In this way, F = e + 2-v can be obtained by finding the vertex number V and edge number E.

The node of the plan consists of the original node and the new node. Because there may be three-line common vertices, You need to delete the repeated vertices.

#include<cstdio>#include<cmath>#include<algorithm>using namespace std;const int N = 300 + 5;struct Point {    double x, y;    Point(double x = 0, double y = 0) : x(x), y(y) { }};typedef Point Vector;Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }Vector operator - (Point A, Point B) { return Vector(A.x - B.x, A.y - B.y); }Vector operator * (Vector A, double p) { return Vector(A.x * p, A.y * p); }Vector operator / (Vector A, double p) { return Vector(A.x/p, A.y/p); }bool operator < (const Point& a, const Point& b) {    return a.x < b.x || (a.x == b.x && a.y < b.y);}const double eps = 1e-10;int dcmp(double x) {    if(fabs(x) < eps) return 0;    else return x < 0 ? -1 : 1;}bool operator == (const Point& a, const Point& b) {    return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;}double Dot(Vector A, Vector B) { return A.x * B.x + A.y * B.y; }double Cross(Vector A, Vector B) { return A.x * B.y - A.y * B.x; }Point GetLineIntersection(Point P, Vector v, Point Q, Vector w) {    Vector u = P - Q;    double t = Cross(w, u) / Cross(v, w);    return P + v * t;}bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2) {    double c1 = Cross(a2-a1, b1-a1), c2 = Cross(a2-a1, b2-a1),           c3 = Cross(b2-b1, a1-b1), c4 = Cross(b2-b1, a2-b1);    return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0;}bool OnSegment(Point p, Point a1, Point a2) {    return dcmp(Cross(a1-p, a2-p)) == 0 && dcmp(Dot(a1-p, a2-p)) < 0;}Point P[N], V[N*N];int main(){    int n, cas = 0;    while(~scanf("%d",&n) && n) {        for(int i = 0; i < n; i++) {            scanf("%lf%lf", &P[i].x, &P[i].y);            V[i] = P[i];        }        n--;        int vcnt = n, ecnt = n;        for(int i = 0; i < n; i++)            for(int j = i + 1; j < n; j++) {                if(SegmentProperIntersection(P[i], P[i+1], P[j], P[j+1]))                    V[vcnt++] = GetLineIntersection(P[i], P[i+1]-P[i], P[j], P[j+1]-P[j]);            }                    sort(V, V+vcnt);        vcnt = unique(V, V+vcnt) - V;                for(int i = 0; i < vcnt; i++)            for(int j = 0; j < n; j++)                if(OnSegment(V[i], P[j], P[j+1]))                    ecnt++;        int ans = ecnt + 2 - vcnt;        printf("Case %d: There are %d pieces.\n", ++cas, ans);    }    return 0;}


La 3236 that nice Euler Circuit (Euler's theorem)

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.