Fishnet
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.
n
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
2
0.2000000 0.6000000
0.3000000 0.8000000
0.1000000 0.5000000
0.5000000 0.6000000
2
0.3333330 0.6666670
0.3333330 0.6666670
0.3333330 0.6666670
0.3333330 0.6666670
4
0.2000000 0.4000000 0.6000000 0.8000000
0.1000000 0.5000000 0.6000000 0.9000000
0.2000000 0.4000000 0.6000000 0.8000000
0.1000000 0.5000000 0.6000000 0.9000000
2
0.5138701 0.9476283
0.1717362 0.1757412
0.3086521 0.7022313
0.2264312 0.5345343
1
0.4000000
0.6000000
0.3000000
0.5000000
0
Sample Output
0.215657
0.111112
0.078923
0.279223
0.348958
题意:求出被网丝切出来的四边形的最大面积
代码:
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define maxn 40
struct point
{
double x, y;
};
point a[maxn], b[maxn], c[maxn], d[maxn], map[maxn][maxn];
void read(int n)
{
int j;
for (j = 1; j <= n; j++)
{
scanf("%lf", &a[j].x);
a[j].y = 0;
}
for (j = 1; j <= n; j++)
{
scanf("%lf", &b[j].x);
b[j].y = 1;
}
for (j = 1; j <= n; j++)
{
scanf("%lf", &c[j].y);
c[j].x = 0;
}
for (j = 1; j <= n; j++)
{
scanf("%lf", &d[j].y);
d[j].x = 1;
}
for (j = 1; j <= n/2; j++)
{
double temp;
temp = c[j].y;
c[j].y = c[n+1-j].y;
c[n+1-j].y = temp;
temp = d[j].y;
d[j].y = d[n+1-j].y;
d[n+1-j].y = temp;
}
}
point GetCrossPoint(point p1, point p2, point q1, point q2)//获取交点
{
point crossPoint;
double tempLeft,tempRight;
tempLeft = 1.0 * (q2.x - q1.x) * (p1.y - p2.y) - (p2.x - p1.x) * (q1.y - q2.y);
tempRight = 1.0 * (p1.y - q1.y) * (p2.x - p1.x) * (q2.x - q1.x) + q1.x * (q2.y - q1.y) * (p2.x - p1.x) - p1.x * (p2.y - p1.y) * (q2.x - q1.x);
crossPoint.x = 1.0 * tempRight / tempLeft;
tempLeft = 1.0 * (p1.x - p2.x) * (q2.y - q1.y) - (p2.y - p1.y) * (q1.x - q2.x);
tempRight = 1.0 * p2.y * (p1.x - p2.x) * (q2.y - q1.y) + (q2.x- p2.x) * (q2.y - q1.y) * (p1.y - p2.y) - q2.y * (q1.x - q2.x) * (p2.y - p1.y);
crossPoint.y = 1.0 * tempRight / tempLeft;
return crossPoint;
}
//double dis(point p1, point p2)
//{
// return sqrt(1.0 * (p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y)) ;
//}
//double ss(double a, double b, double c)
//{
// double p = 1.0 * (a + b + c) / 2;
// return p = sqrt(1.0 * p * (p-a) * (p-b) * (p-c));
//}
//double squal(point pp1, point pp2, point pp3, point pp4)
//{
// double a, b, c, d, e;
// a = dis(pp1, pp2);
// b = dis(pp2, pp4);
// d = dis(pp4, pp3);
// c = dis(pp3, pp1);
// e = dis(pp1, pp4);
// return 1.0 * (ss(a, b, e) + ss(d, c, e));
//}
double cj(point p1, point p2)
{
return 1.0 * p1.x * p2.y - p1.y * p2.x;
}
double squal(point p1, point p2, point p3, point p4)
{
double sum = 0.5 * (cj(p1, p2) + cj(p2, p4) + cj(p4, p3) + cj(p3, p1));
if (sum < 0)
{
return -sum;
}
return sum;
}
//double squal(point p1, point p2, point p3, point p4)
//{
// double x1, y1, x2, y2, a, b, sum;
// //----------------------------------叉积算面积,算出来为三角形的两倍
// x1 = p1.x - p3.x, y1 = p1.y - p3.y;
// x2 = p4.x - p3.x, y2 = p4.y - p3.y;
// a = 1.0 * x1 * y2 - y1 * x2;
// if (a < 0)
// {
// a = -a;
// }
// //-----------------------------------
// x1 = p1.x - p2.x, y1 = p1.y - p2.y;
// x2 = p4.x - p2.x, y2 = p4.y - p2.y;
// b = 1.0 * x1 * y2 - y1 * x2;
// if (b < 0)
// {
// b = -b;
// }
// return sum = 1.0 * (a + b) / 2;
//}
double deal(int n)
{
int i, j;
double max, pre;
point ans;
map[0][0].x = 0, map[0][0].y = 1;
map[n+1][0].x = 0, map[n+1][0].y = 0;
map[0][n+1].x = 1, map[0][n+1].y = 1;
map[n+1][n+1].x = 1, map[n+1][n+1].y = 0;
for (i = 1; i <= n; i++)
{
map[0][i] = b[i];
map[n+1][i] = a[i];
map[i][0] = c[i];
map[i][n+1] = d[i];
}
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
map[i][j] = GetCrossPoint(c[i], d[i], b[j], a[j]);
}
}
for (i = 0, max = 0; i <= n; i++)
{
for (j = 0; j <= n; j++)
{
pre = squal(map[i][j], map[i][j+1], map[i+1][j], map[i+1][j+1]);
if (pre > max)
{
max = pre;
}
}
}
return max;
}
int main()
{
int n;
double ans;
while (scanf("%d", &n), n)
{
read(n);
ans = deal(n);
printf("%.6lf\n", ans);
}
system("pause");
return 0;
}