poj1408: 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
题目大意
在边长为1的正方形中有横向和纵向的若干条不相交的边,求这些边形成最大四边形的面积。
知识点:定比分点求线段交点
定比分点定理
设坐标轴上有向线段
AB→
的起点A和终点B的横坐标分别为
x1
和
x2
,分点M分
AB→
的比为
λ
,那么,分点M的坐标
x=x1+λ⋅x21+λ
证明从略,自行百度
我们考虑两条相交线段
λ=|DP||PC|=SΔABDSΔABC=|AB→×AD→||AC→×AB→|
⇒xp=Area(A,B,D)⋅xc+Area(A,B,C)⋅xdArea(A,B,D)+Area(A,B,C)
可以比较轻松地用叉积解决,代码量也可以接受。
拓展:
对于未知是否相交的线段:
快速排斥,跨立判定,定比分点求交点。
对于直线:
取点求交点。
题目分析
枚举每个交点,计算其右下角的四边形面积并取最大值即可。
代码
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<cmath>
using namespace std;
double ans; int n;
struct point {
double x, y;
point(double xx = 0, double yy = 0) : x(xx), y(yy) {}
double operator * (point a) {return x * a.y - y * a.x;}
point operator * (double a) {return point(x * a, y * a);}
point operator - (point a) {return point(x - a.x, y - a.y);}
point operator + (point a) {return point(x + a.x, y + a.y);}
point operator / (double a) {return point(x / a, y / a);}
};
double area(point p1, point p2, point p3) {return fabs((p2 - p1) * (p3 - p1));}
double area(point p1, point p2, point p3, point p4) {
return (area(p1, p2, p3) + area(p2, p3, p4)) / 2;
}
struct line {
point a, b;
line(point aa = point(0, 0), point bb = point(0, 0)) : a(aa), b(bb) {}
point operator + (line c) {
double w1 = area(a, b, c.a), w2 = area(a, b, c.b);
return point((c.a * w2 + c.b * w1) / (w1 + w2));
}
}la[35], lb[35];
bool init() {
scanf("%d", &n); if(!n) return false;
for(int i = 1;i <= n; ++i) scanf("%lf", &la[i].a.x), la[i].a.y = 0;
for(int i = 1;i <= n; ++i) scanf("%lf", &la[i].b.x), la[i].b.y = 1;
for(int i = 1;i <= n; ++i) scanf("%lf", &lb[i].a.y), lb[i].a.x = 0;
for(int i = 1;i <= n; ++i) scanf("%lf", &lb[i].b.y), lb[i].b.x = 1;
++n;
la[n] = line(point(1, 0), point(1, 1));
la[0] = line(point(0, 0), point(0, 1));
lb[n] = line(point(0, 1), point(1, 1));
}
int main() {
while(init()) {
ans = 0;
for(int i = 1;i <= n; ++i) {
point p1 = la[i - 1].a, p2 = la[i].a;
for(int j = 1;j <= n; ++j) {
point p3 = la[i - 1] + lb[j], p4 = la[i] + lb[j];
ans = max(ans, area(p1, p2, p3, p4));
p1 = p3; p2 = p4;
}
}
printf("%.6lf\n", ans);
}
return 0;
}