您的位置:首页 > 其它

poj_1408 Fishnet(四边形面积)

2016-11-22 20:28 387 查看
Fishnet
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 2195 Accepted: 1387
DescriptionA 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 ofhis 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 wellas 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 enoughfor neglecting its thickness.InputThe 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.na1 a2 ... anb1 b2 ... bnc1 c2 ... cnd1 d2 ... dnyou may assume 0 < n <= 30, 0 < ai,bi,ci,di < 1OutputFor 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 <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <queue>#include <set>#include <map>#include <string>#include <algorithm>#define FOP freopen("data.txt","r",stdin)#define FOP2 freopen("data1.txt","w",stdout)#define inf 0x3f3f3f3f#define maxn 1000010#define mod 1000000007#define PI acos(-1.0)#define LL long longusing namespace std;struct Point{double x, y;Point(double x = 0, double y = 0) : x(x), y(y) { }};typedef Point Vector;Vector operator + (Vector A, Vector B){return Vector(A.x + B.x, A.y + B.y);}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, double p){return Vector(A.x / p, A.y / p);}bool operator < (const Point& a, const Point& b){return a.x < b.y || (a.x == b.x && a.y < b.y);}const double eps = 1e-10;int dcmp(double x){if(fabs(x) < eps) return 0;else return x < 0 ? -1 : 1;}bool operator == (const Point& a, const Point& b){return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;}double Dot(Vector A, Vector B){return A.x * B.x + A.y * B.y;}double Length(Vector A){return sqrt(Dot(A, A));}double Angle(Vector A, Vector B){return acos(Dot(A, B) / Length(A) / Length(B));}double Cross(Vector A, Vector B){return A.x * B.y - A.y * B.x;}double Area2(Point A, Point B, Point C){return Cross(B - A, C - A);}Point GetLineIntersection(Point P, Vector v, Point Q, Vector w){Vector u = P - Q;double t = Cross(w, u) / Cross(v, w);Point ans = P + v * t;return P + v * t;}double PolygonArea(Point* p, int n){double area = 0;for(int i = 1; i < n - 1; i++)area += Cross(p[i] - p[0], p[i+1] - p[0]);return area / 2;}Point get_point(){double x, y;scanf("%lf%lf", &x, &y);return Point(x, y);}int n;Point P[50][50];int main(){while(~scanf("%d", &n) && n){double ans = 0, d;P[0][0] = Point(0, 0), P[0][n+1] = Point(0, 1), P[n+1][0] = Point(1, 0), P[n+1][n+1] = Point(1, 1);for(int i = 1; i <= n; i++) scanf("%lf", &d), P[0][i] = Point(0, d);for(int i = 1; i <= n; i++) scanf("%lf", &d), P[n+1][i] = Point(1, d);for(int i = 1; i <= n; i++) scanf("%lf", &d), P[i][0] = Point(d, 0);for(int i = 1; i <= n; i++) scanf("%lf", &d), P[i][n+1] = Point(d, 1);for(int i = 1; i <= n; i++)for(int j = 1; j <= n; j++)P[j][i] = GetLineIntersection(P[0][i], P[n+1][i]-P[0][i], P[j][0], P[j][n+1]-P[j][0]);Point t[4];for(int i = 0; i <= n; i++){for(int j = 0; j <= n; j++){t[0] = P[i][j], t[1] = P[i+1][j], t[2] = P[i+1][j+1], t[3] = P[i][j+1];double x = PolygonArea(t, 4);if(x > ans) ans = x;}}printf("%.6f\n", ans);}return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: