首页 > 代码库 > POJ1408-Fishnet(线段求交)
POJ1408-Fishnet(线段求交)
Fishnet
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 1788 | Accepted: 1144 |
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.
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
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
题意:题上的图很明显了。
思路:线段求交(把边上的线段算上),得到的4个点算面积。
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <vector> #include <algorithm> using namespace std; #define REP(_,a,b) for(int _ = (a); _ < (b); _++) #define sz(s) (int)((s).size()) typedef long long ll; const double eps = 1e-10; const int maxn = 50; int n; struct Point{ double x,y; Point(double x=0.0,double y = 0.0):x(x),y(y){} }; Point vP[maxn]; typedef Point Vector; struct Line { Point P; Vector v; double ang; Line(){} Line(Point P,Vector v):P(P),v(v){ ang = atan2(v.y,v.x); } bool operator <(const Line&L) const{ return ang < L.ang; } }; Line LX[maxn],LY[maxn]; Vector operator + (Vector A,Vector B) { return Vector(A.x+B.x,A.y+B.y); } Vector operator - (Vector A,Vector 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); } 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.x-b.x)==0&&dcmp(a.y-b.y)<0; } 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;} Vector Rotate(Vector A,double rad) {return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad)); } Vector Normal(Vector A) { double L = Length(A); return Vector(-A.y/L,A.x/L); } bool OnSegment(Point p,Point a1,Point a2){ return dcmp(Cross(a1-p,a2-p)) == 0 && dcmp(Dot(a1-p,a2-p)) < 0; } Point GetIntersection(Line a,Line b){ Vector u = a.P-b.P; double t = Cross(b.v,u) / Cross(a.v,b.v); return a.P+a.v*t; } int ConvexHull(Point* p,int n,Point *ch){ sort(p,p+n); int m = 0; for(int i = 0; i < n; i++) { while(m > 1 && dcmp(Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])) <= 0) m--; ch[m++] = p[i]; } int k = m; for(int i = n-2; i >= 0; i--){ while(m > k && dcmp(Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])) <= 0) m--; ch[m++] = p[i]; } if(n > 1) m--; return m; } double a[maxn]; void input(){ double t; REP(i,0,n) scanf("%lf",&a[i]); LX[0] = Line(Point(0,1.0),Vector(0,-1.0)); REP(i,0,n){ scanf("%lf",&t); LX[i+1] = Line(Point(t,1.0),Vector(a[i]-t,-1.0)); } LX[n+1] = Line(Point(1.0,1.0),Vector(0,-1.0)); LY[0] = Line(Point(1.0,0),Vector(-1.0,0)); REP(i,0,n) scanf("%lf",&a[i]); REP(i,0,n){ scanf("%lf",&t); LY[i+1] = Line(Point(1.0,t),Vector(-1.0,a[i]-t)); } LY[n+1] = Line(Point(1.0,1.0),Vector(-1.0,0)); } void solve(){ double ans = 0; REP(i,1,n+2) { Point a,b,c,d; REP(j,1,n+2) { a = GetIntersection(LX[i-1],LY[j-1]); //cout<<a.x<<" "<<a.y<<endl; b = GetIntersection(LX[i],LY[j-1]); //cout<<b.x<<" "<<a.y<<endl; c = GetIntersection(LX[i],LY[j]); d = GetIntersection(LX[i-1],LY[j]); double area = 0; area = fabs(Cross(a,b)/2+Cross(b,c)/2+Cross(c,d)/2+Cross(d,a)/2); ans = max(area,ans); } } printf("%.6f\n",ans); } int main(){ while(~scanf("%d",&n) && n){ input(); solve(); } return 0; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。