poj 1408 Fishnet

来源:互联网 发布:中盟网络快运 编辑:程序博客网 时间:2024/05/17 02:47

题目链接:点击打开链接

Description

A fisherman named Etadokah awoke in a very small island. He could see calm, beautiful and blue sea around the island. The previous night he had encountered a terrible storm and had reached this uninhabited island. Some wrecks of his ship were spread around him. He found a square wood-frame and a long thread among the wrecks. He had to survive in this island until someone came and saved him. 

In order to catch fish, he began to make a kind of fishnet by cutting the long thread into short threads and fixing them at pegs on the square wood-frame. He wanted to know the sizes of the meshes of the fishnet to see whether he could catch small fish as well as large ones. 

The wood frame is perfectly square with four thin edges on meter long: a bottom edge, a top edge, a left edge, and a right edge. There are n pegs on each edge, and thus there are 4n pegs in total. The positions of pegs are represented by their (x,y)-coordinates. Those of an example case with n=2 are depicted in figures below. The position of the ith peg on the bottom edge is represented by (ai,0). That on the top edge, on the left edge and on the right edge are represented by (bi,1), (0,ci) and (1,di), respectively. The long thread is cut into 2n threads with appropriate lengths. The threads are strained between (ai,0) and (bi,1),and between (0,ci) and (1,di) (i=1,...,n). 

You should write a program that reports the size of the largest mesh among the (n+1)2 meshes of the fishnet made by fixing the threads at the pegs. You may assume that the thread he found is long enough to make the fishnet and the wood-frame is thin enough for neglecting its thickness. 
 

Input

The input consists of multiple sub-problems followed by a line containing a zero that indicates the end of input. Each sub-problem is given in the following format. 

a1 a2 ... an 
b1 b2 ... bn 
c1 c2 ... cn 
d1 d2 ... dn 
you may assume 0 < n <= 30, 0 < ai,bi,ci,di < 1

Output

For each sub-problem, the size of the largest mesh should be printed followed by a new line. Each value should be represented by 6 digits after the decimal point, and it may not have an error greater than 0.000001.

Sample Input

20.2000000 0.60000000.3000000 0.80000000.1000000 0.50000000.5000000 0.600000020.3333330 0.66666700.3333330 0.66666700.3333330 0.66666700.3333330 0.666667040.2000000 0.4000000 0.6000000 0.80000000.1000000 0.5000000 0.6000000 0.90000000.2000000 0.4000000 0.6000000 0.80000000.1000000 0.5000000 0.6000000 0.900000020.5138701 0.94762830.1717362 0.17574120.3086521 0.70223130.2264312 0.534534310.40000000.60000000.30000000.50000000

Sample Output

0.2156570.1111120.0789230.2792230.348958

知识点:求多边形的面积,两条直线的交点,叉积

题目大意:给出一个1*1的木制的边框,在边框上有4*n割钉子,每个边框上有n个,用线将左右边框上的钉子按着从上往下连起来,上下边框上的钉子按着从左往右连起来,够成了一个网状的,求这个网状的框架中网格面积最大的是多少;

基本思路:这个题如果知道基本的集合问题就不难想到了(我最近在看几何的知识)。先考虑怎么求面积,可以想到用叉积,但是用叉积要用到多边形的每个点,从而问题又转化为求每两条边的交点,;

ps: 求两条边的交点时,有公式的,(算法入门-训练指南的p257上面讲的很详细,只是没有证明)

    求多边形的面积更是简单了(当然思路是简单的,其中的证明公式的来源是苦逼的):先定住一个点 然后顺时针或逆时针遍历相邻的两个点,然后计算其与所顶点构成的叉积的和就是该多边形的两倍(两向量的叉积是该向量所构成的平行四边形的面积)

<span style="font-size:18px;">#include <iostream>#include<cstring>#include<cstdio>using namespace std;struct point{    double x,y;    point(double x=0,double y=0) :x(x),y(y) { }}a[50],b[50],c[50],d[50];typedef point Vector;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,Vector B) {return Vector(A.x+B.x,A.y+B.y);}double cross(Vector A,Vector B){    return A.x*B.y-A.y*B.x;}point Getlineintersection(point p,Vector v,point q,Vector w){    Vector u=p-q;    double t=cross(w,u)/cross(v,w);    return p+v*t;}int main(){    int n;    double x,y;    while(cin>>n,n)    {        a[0].x=0;        a[0].y=0;        for(int i=1;i<=n;i++)        {            cin>>x;            a[i].x=x;            a[i].y=0;        }        a[n+1].x=1;        a[n+1].y=0;        b[0].x=0;        b[0].y=1;        for(int i=1;i<=n;i++)        {            cin>>x;            b[i].x=x;            b[i].y=1;        }        b[n+1].x=1;        b[n+1].y=1;        c[0].x=0;        c[0].y=0;       for(int i=1;i<=n;i++)       {           cin>>y;           c[i].y=y;           c[i].x=0;       }       c[n+1].x=0;       c[n+1].y=1;       d[0].x=1;       d[0].y=0;       for(int i=1;i<=n;i++)       {           cin>>y;           d[i].y=y;           d[i].x=1;       }       d[n+1].x=1;       d[n+1].y=1;       //point  pre[],next;       double s=-1;       point mid[50];       for(int i=1;i<=n+1;i++)///(c->d)       {           mid[0]=c[i];           for(int j=1;j<=n;j++)///(a->b)           {               mid[j]=Getlineintersection(c[i],d[i]-c[i],a[j],b[j]-a[j]);           }           mid[n+1]=d[i];           double area=0;           for(int j=0;j<=n;j++)           {               area=cross(a[j+1]-a[j],mid[j+1]-a[j])+cross(mid[j+1]-a[j],mid[j]-a[j]);               area/=2;               if(area>s)s=area;           }           for(int j=0;j<=n+1;j++)           {               a[j]=mid[j];           }       }       printf("%.6f\n",s);    }    return 0;}</span>


0 0
原创粉丝点击