LA2402暴力枚举+计算几何+四边形面积

  1 /*
  2 LA2402:
  3 题意:在矩形中给定横线(竖)之间不交叉的n对线,求被分割的小块的最大面积
  4 读懂题意就可以发现是思路很简单的题目
  5 枚举+几何计算,时间复杂度度不高
  6 熟悉了部分函数的运用
  7 
  8 */
  9 #include <stdio.h>
 10 #include <stdlib.h>
 11 #include <string.h>
 12 #include <math.h>
 13 #include <ctype.h>
 14 #include <string>
 15 #include <iostream>
 16 #include <sstream>
 17 #include <vector>
 18 #include <queue>
 19 #include <stack>
 20 #include <map>
 21 #include <list>
 22 #include <set>
 23 #include <algorithm>
 24 
 25 using namespace std;
 26 
 27 struct Point
 28 {
 29     double x,y;
 30     Point(double x=0,double y=0):x(x),y(y){}
 31     void print()
 32     {
 33         cout<<"("<<x<<","<<y<<")"<<endl;
 34     }
 35 };
 36 
 37 typedef Point Vector;
 38 
 39 Vector operator-(Point A,Point B)//表示A指向B
 40 {
 41     return Vector(A.x-B.x,A.y-B.y);
 42 }
 43 Vector operator*(Vector A,double k)
 44 {
 45     return Vector(A.x*k,A.y*k);
 46 }
 47 Vector operator+(Point A,Point B)//表示A指向B
 48 {
 49     return Vector(B.x+A.x,B.y+A.y);
 50 }
 51 double Cross(Vector A,Vector B) {return A.x*B.y-A.y*B.x;}
 52 double Area(Point A,Point B,Point C)//三角形面积
 53 {
 54     return  fabs(Cross(B-A,C-A))/2;
 55 }
 56 struct Line
 57 {
 58     Point p;
 59     Vector v;
 60     Line(Point p,Vector v):p(p),v(v){}
 61 };
 62 Line Getline(Point A,Point B)//求直线AB
 63 {
 64     Vector u=A-B;
 65     return Line(A,u);
 66 }
 67 Point InterSection(Line L1,Line L2)//求直线交点
 68 {
 69     Vector u=L1.p-L2.p;
 70     double t=Cross(L2.v,u)/Cross(L1.v,L2.v);
 71     return L1.p+L1.v*t;
 72 }
 73 double a[50],b[50],c[50],d[50];
 74 int n;
 75 int main()
 76 {
 77     while(cin>>n)
 78     {
 79         if(!n) break;
 80         for(int i=1;i<=n;i++) cin>>a[i];
 81         for(int i=1;i<=n;i++) cin>>b[i];
 82         for(int i=1;i<=n;i++) cin>>c[i];
 83         for(int i=1;i<=n;i++) cin>>d[i];
 84         a[0]=0;b[0]=0;c[0]=0;d[0]=0;
 85         a[n+1]=1;b[n+1]=1;c[n+1]=1;d[n+1]=1;
 86         double m=-1;
 87         for(int i=0;i<=n;i++)//外层枚举相邻的横线
 88         {
 89             Line L1=Getline(Point(0,c[i]),Point(1,d[i]));
 90             Line L2=Getline(Point(0,c[i+1]),Point(1,d[i+1]));
 91             for(int j=0;j<=n;j++)//内层枚举相邻的竖线
 92             {
 93             Line L3=Getline(Point(a[j],0),Point(b[j],1));//一开始写成i,= =!
 94             Line L4=Getline(Point(a[j+1],0),Point(b[j+1],1));
 95             Point P1=InterSection(L1,L3);
 96             Point P2=InterSection(L1,L4);
 97             Point P3=InterSection(L2,L4);
 98             Point P4=InterSection(L2,L3);
 99             double S=Area(P1,P2,P4)+Area(P2,P3,P4);
100 //            P1.print();P2.print();P3.print();P4.print();
101 //            cout<<"A1="<<Area(P1,P2,P4)<<endl;
102 //            cout<<"A2="<<Area(P2,P3,P4)<<endl;
103 //            cout<<"S="<<S<<endl;
104             if (S>m) m=S;
105             }
106         }
107         printf("%.6lf\n",m);
108     }
109     return 0;
110 }

 

posted @ 2014-02-26 21:00  little_w  阅读(208)  评论(0编辑  收藏  举报