您的位置:首页 > 其它

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

2014-02-26 21:00 246 查看
/*
LA2402:
题意:在矩形中给定横线(竖)之间不交叉的n对线,求被分割的小块的最大面积
读懂题意就可以发现是思路很简单的题目
枚举+几何计算,时间复杂度度不高
熟悉了部分函数的运用

*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <string>
#include <iostream>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <list>
#include <set>
#include <algorithm>

using namespace std;

struct Point
{
double x,y;
Point(double x=0,double y=0):x(x),y(y){}
void print()
{
cout<<"("<<x<<","<<y<<")"<<endl;
}
};

typedef Point Vector;

Vector operator-(Point A,Point B)//表示A指向B
{
return Vector(A.x-B.x,A.y-B.y);
}
Vector operator*(Vector A,double k)
{
return Vector(A.x*k,A.y*k);
}
Vector operator+(Point A,Point B)//表示A指向B
{
return Vector(B.x+A.x,B.y+A.y);
}
double Cross(Vector A,Vector B) {return A.x*B.y-A.y*B.x;}
double Area(Point A,Point B,Point C)//三角形面积
{
return  fabs(Cross(B-A,C-A))/2;
}
struct Line
{
Point p;
Vector v;
Line(Point p,Vector v):p(p),v(v){}
};
Line Getline(Point A,Point B)//求直线AB
{
Vector u=A-B;
return Line(A,u);
}
Point InterSection(Line L1,Line L2)//求直线交点
{
Vector u=L1.p-L2.p;
double t=Cross(L2.v,u)/Cross(L1.v,L2.v);
return L1.p+L1.v*t;
}
double a[50],b[50],c[50],d[50];
int n;
int main()
{
while(cin>>n)
{
if(!n) break;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1;i<=n;i++) cin>>b[i];
for(int i=1;i<=n;i++) cin>>c[i];
for(int i=1;i<=n;i++) cin>>d[i];
a[0]=0;b[0]=0;c[0]=0;d[0]=0;
a[n+1]=1;b[n+1]=1;c[n+1]=1;d[n+1]=1;
double m=-1;
for(int i=0;i<=n;i++)//外层枚举相邻的横线
{
Line L1=Getline(Point(0,c[i]),Point(1,d[i]));
Line L2=Getline(Point(0,c[i+1]),Point(1,d[i+1]));
for(int j=0;j<=n;j++)//内层枚举相邻的竖线
{
Line L3=Getline(Point(a[j],0),Point(b[j],1));//一开始写成i,= =!
Line L4=Getline(Point(a[j+1],0),Point(b[j+1],1));
Point P1=InterSection(L1,L3);
Point P2=InterSection(L1,L4);
Point P3=InterSection(L2,L4);
Point P4=InterSection(L2,L3);
double S=Area(P1,P2,P4)+Area(P2,P3,P4);
//            P1.print();P2.print();P3.print();P4.print();
//            cout<<"A1="<<Area(P1,P2,P4)<<endl;
//            cout<<"A2="<<Area(P2,P3,P4)<<endl;
//            cout<<"S="<<S<<endl;
if (S>m) m=S;
}
}
printf("%.6lf\n",m);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: