大意:在Dukeswood这块土地上生活着一个富有的农庄主和他的几个孩子。在他临终时,他想把他的土地分给他的孩子。他有许多农场,每个农场都是一块矩形土地。他在农场地图上划上一些直线将矩形分成若干块。当他划直线时,他总是从矩形边界上的某一点划到另一个矩形边界上的点,这条线的结束点将成为下一条线的起始点。他划线时从不会让任三线共点。题目链接
思路:设f(n)为前n条输入线段将矩形分成区域的个数。
边界:f(1) = 2,假设已经产生了n-1条线段,新线段为l,它和已有的n-1条线段有T(n)个交点,这些交点将l分成了T(n)+1条线段,线段将所在区域一分为2,所以新增了T(n)+1个区域,所以f(n) = f(n-1)+T(n)+1,递推可知:f(L) = f(1) + T+L-1 = T+L+1,其中L为总的线段相交的交点数目。
#include <iostream> #include <cstdlib> #include <cstdio> #include <string> #include <cstring> #include <cmath> #include <vector> #include <queue> #include <stack> using namespace std; const double eps = 1e-10; 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) { if(a.x != b.x) return a.x < b.x; return a.y < b.y; } 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 Length(Vector A) { return sqrt(Dot(A, A)); } double Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); } double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; } double Area2(Point A, Point B, Point C) { return Cross(B-A, C-A); } Vector Rotate(Vector A, double rad) { return Vector(A.x*cos(rad)-A.y*sin(rad), A.x*sin(rad) + A.y*cos(rad)); } Point GetIntersection(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); double c3 = Cross(b2-b1, a1-b1), c4 = Cross(b2-b1, a2-b1); return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0; } 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; } Point read_point() { Point A; scanf("%lf%lf", &A.x, &A.y); return A; } int w, h; int n; const int maxn = 60; Point p[maxn]; int read_case() { scanf("%d%d", &w, &h); if(!w) return 0; scanf("%d", &n); for(int i = 0; i <= n; i++) p[i] = read_point(); return 1; } void solve() { int tot = 0; for(int i = 0; i <= n-1; i++) { for(int j = i+1; j <= n-1; j++) { Point a1 = p[i], a2 = p[i+1]; Point b1 = p[j], b2 = p[j+1]; if(SegmentProperIntersection(a1, a2, b1, b2)) tot++; } } int ans = n+tot+1; printf("%d\n", ans); } int main() { while(read_case()) { solve(); } return 0; }