题意:一笔画,问该图形将平面分成多少个区域
分析:训练指南P260,欧拉定理:平面图定点数V,边数E,面数F,则V + F - E = 2。那么找出新增的点和边就可以了。用到了判断线段相交,求交点,判断点在线上
/************************************************ * Author :Running_Time * Created Time :2015/10/22 星期四 09:10:09 * File Name :LA_3263.cpp ************************************************/ #include <cstdio> #include <algorithm> #include <iostream> #include <sstream> #include <cstring> #include <cmath> #include <string> #include <vector> #include <queue> #include <deque> #include <stack> #include <list> #include <map> #include <set> #include <bitset> #include <cstdlib> #include <ctime> using namespace std; #define lson l, mid, rt << 1 #define rson mid + 1, r, rt << 1 | 1 typedef long long ll; const int N = 300 + 10; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; const double EPS = 1e-10; struct Point { double x, y; Point (double x=0, double y=0) : x (x), y (y) {} }; typedef Point Vector; 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; } int dcmp(double x) { if (fabs (x) < EPS) return 0; else return x < 0 ? -1 : 1; } Vector operator + (Vector A, Vector B) { return Vector (A.x + B.x, A.y + B.y); } Vector operator - (Vector A, Vector 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); } bool operator == (const Point &a, const Point &b) { return dcmp (a.x - b.x) == 0 && dcmp (a.y - b.y) == 0; } double length(Vector A) { return sqrt (dot (A, A)); } Point point_inter(Point p, Vector V, Point q, Vector W) { Vector U = p - q; double t = cross (W, U) / cross (V, W); return p + V * t; } Point point_proj(Point p, Point a, Point b) { Vector V = b - a; return a + V * (dot (V, p - a) / dot (V, V)); } bool inter(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 on_seg(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(void) { int n, cas = 0; while (scanf ("%d", &n) == 1) { if (!n) break; for (int i=0; i<n; ++i) { scanf ("%lf%lf", &P[i].x, &P[i].y); V[i] = P[i]; } n--; int v = n, e = n; for (int i=0; i<n; ++i) { for (int j=i+1; j<n; ++j) { if (inter (P[i], P[i+1], P[j], P[j+1])) { V[v++] = point_inter (P[i], P[i+1] - P[i], P[j], P[j+1] - P[j]); } } } sort (V, V+v); v = unique (V, V+v) - V; for (int i=0; i<v; ++i) { for (int j=0; j<n; ++j) { if (on_seg (V[i], P[j], P[j+1])) e++; } } printf ("Case %d: There are %d pieces.\n", ++cas, e + 2 - v); } return 0; }
时间: 2024-10-13 00:27:35