This paper uses the Euler's formula of the plan: set the number of vertices, number of edges and number of faces to V, E, F, then V + F-E = 2. 1. when finding the number of vertices V, it is easy to think of the idea of judging the intersection of two line segments. If the two line segments intersect, the number of vertices is + 1. However, the two vertices may overlap, therefore, the coordinates of the intersection must be generated. Remove the duplicate vertex, that is, remove the vertex with the same coordinate value, then the array size is V. Sort by sort and then remove duplicates by unique. 2. when finding E, you need to judge the vertex in each V. If the vertex c online segment (a, B) does not contain the endpoint, the number of line segments is + 1, after the result is obtained, add the number of initialized line segments. 3. Evaluate F Based on Euler's formula. [Cpp] # include <iostream> # include <stdio. h> # include <stdlib. h> # include <string. h> # include <math. h ># include <algorithm> using namespace std; const double eps = 1e-8; struct Point {double x; double y; Point () {} Point (double _ x, double _ y): x (_ x), y (_ y) {} friend Point operator + (Point a, Point B) {return Point (. x + B. x,. y + B. y);} friend Point operator-(Point a, Point B) {return Point (. x-b.x ,. Y-b.y) ;}}; struct Edge {Point a; Point B ;}; Edge e [315]; Point p_temp [315]; Point p [100000]; int p_num = 0; int dcmp (double x) // three-state function {if (fabs (x) <eps) // within a certain precision range, it can be considered as 0 return 0; return x> 0? 1:-1;} double det (Point a, Point B) // cross product, heavy load cross product function {return. x * B. y-a.y * B. x;} double det (Point a, Point B, Point o) // Cross Product {return det (a-o, B-o);} double det (Point, point B, Point c, Point d) // Cross Product {return det (B-a, d-c);} double dot (Point a, Point B) // Point product {return. x * B. x +. y * B. y;} double dot (Point a, Point B, Point o) // Point product {return dot (a-o, B-o);} void intersect (Point a, Point o, point B, Point c, Point d) // judge Whether the disconnection segment is intersecting {int d1 = dcmp (det (a, B, c); int d2 = dcmp (det (a, B, d )); int d3 = dcmp (det (c, d, a); int d4 = dcmp (det (c, d, B); Point temp; // obtain the intersection coordinate if (d1 * d2 <0 & d3 * d4 <0) {double t = (. x-c.x) * (c. y-d.y)-(. y-c.y) * (c. x-d.x)/(. x-b.x) * (c. y-d.y)-(. y-b.y) * (c. x-d.x); temp. x =. x + (B. x-. x) * t; temp. y =. y + (B. y-. y) * t; p [p_num ++] = temp;} // c else if (d1 = 0 & dcmp (dot (, b, c) <= 0 ){ Temp. x = c. x; temp. y = c. y; p [p_num ++] = temp;} // d on (a, B) else if (d2 = 0 & dcmp (dot (a, B, d) <= 0) {temp. x = d. x; temp. y = d. y; p [p_num ++] = temp;} // a else if (d3 = 0 & dcmp (dot (c, d, a) <= 0) {temp. x =. x; temp. y =. y; p [p_num ++] = temp;} // B else if (d4 = 0 & dcmp (dot (c, d, b) <= 0) {temp. x = B. x; temp. y = B. y; p [p_num ++] = temp ;}// sort bool cmp (Point a, Point B) {if (. x-B. x> eps) {Return true;} else if (fabs (. x-B. x) <eps &. y-B. y> eps) {return true;} return false;} // The two coordinates are equal to bool cmp2 (Point a, Point B) {if (fabs (. x-B. x) <eps & fabs (. y-B. y) <eps) {return true;} return false;} int main () {# ifndef ONLINE_JUDGE freopen ("in.txt", "r", stdin); # endif int n; int num = 0; while (scanf ("% d", & n )! = EOF & n! = 0) {num ++; p_num = 0; for (int I = 1; I <= n; I ++) {scanf ("% lf ", & p_temp [I]. x, & p_temp [I]. y) ;}for (int I = 1; I <n; I ++) {e [I]. a. x = p_temp [I]. x; e [I]. a. y = p_temp [I]. y; e [I]. b. x = p_temp [I + 1]. x; e [I]. b. y = p_temp [I + 1]. y;} n --; int E = n; int V = 0; for (int I = 1; I <n; I ++) {for (int j = I + 1; j <= n; j ++) {intersect (e [I]. a, e [I]. b, e [j]. a, e [j]. b) ;}} sort (p, p + p_num, cmp); p_num = unique (p, p + p_num, cmp2)-p; V = p_num; for (int I = 0; I <V; I ++) {for (int j = 1; j <= n; j ++) {// determine whether the vertex is online. if (dcmp (det (e [j]. a, e [j]. b, p [I]) = 0 & dcmp (dot (e [j]. a, e [j]. b, p [I]) <0) {E ++ ;}} printf ("Case % d: There are % d pieces. \ n ", num, 2 + E-V);} return 0 ;}