• POJ 1408 Fishnet(几何叉积求面积 + 求直线的交点坐标)


    Fishnet

    Time Limit: 1000MS

     

    Memory Limit: 10000K

    Total Submissions: 1302

     

    Accepted: 824

    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

    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

    Source

    Japan 2001

     解题报告:题意求直线划分四边形中最大四边形面积,利用叉积求四边形面积,关键是如何求矩形中间的直线交点的坐标,求出坐标之后就是套用模板了!

    代码如下:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    using namespace std;
    const int MAX = 105;
    int n;
    struct Point
    {
        double x;
        double y;
    };
    Point a[MAX], b[MAX], c[MAX], d[MAX], p[MAX][MAX];
    double area[MAX][MAX], ans;
    Point GetPoint(Point p1, Point p2, Point p3, Point p4)//已知四个点求直线相交的点的坐标
    //(这种求法包括了直线斜率不存在或为0的情况下)
    {
        Point p0;
        double A1 = p2.y - p1.y;
        double B1 = p1.x - p2.x;
        double C1 = p1.y * (-B1) - p1.x * A1;
        double A2 = p4.y - p3.y;
        double B2 = p3.x - p4.x;
        double C2 = p3.y *(-B2) - p3.x * A2;
        p0.x = (C2 * B1 - C1 * B2) / (A1 * B2 - A2 * B1);
        p0.y = (C2 * A1 - C1 * A2) / (B1 * A2 - B2 * A1);
        return p0;
    }
    void GetPoints()
    {
        int i, j;
        //矩形四个角的坐标
        p[1][1].x = 0.0;
        p[1][1].y = 0.0;
        p[1][n + 2].x = 0.0;
        p[1][n + 2].y = 1.0;
        p[n + 2][1].x = 1.0;
        p[n + 2][1].y = 0.0;
        p[n + 2][n + 2].x = 1.0;
        p[n + 2][n + 2].y = 1.0;
        //矩形四条边的上点的坐标
        for (i = 2; i < n + 2; ++i)
        {
            p[i][1] = a[i - 1];//y为0的边(下)
            p[i][n + 2] = b[i - 1];//x为1的边(右)
            p[1][i] = c[i - 1];//x为0的边(左)
            p[n + 2][i] = d[i -1];//y为1的边(上)
        }
        for (i = 2; i < n + 2; ++i)//获取矩阵中间的点的坐标
        {
            for (j = 2; j < n + 2; ++j)
            {
                p[i][j] = GetPoint(p[i][1],p[i][n+2],p[1][j],p[n+2][j]);
            }
        }
    }
    double GetArea(Point p1, Point p2, Point p3, Point p4)//已知四个点的坐标求四边形的面积
    {
        int i, j;
        double ar = 0;
        Point ac[4];
        ac[0] = p1;
        ac[1] = p2;
        ac[2] = p3;
        ac[3] = p4;
        for (i = 0; i < 4; ++i)//叉积求四边形面积
        {
            j = (i + 1) % 4;
            ar += ac[i].x * ac[j].y - ac[i].y * ac[j].x;
        }
        ar = ar / 2.0;//要除以2.0
        if (ar < 0)//面积是正数
        {
            ar = - ar;
        }
        return ar;
    }
    
    void GetAreas()
    {
        memset(area, 0, sizeof(area));
        int i, j;
        for (i = 1; i <= n + 1; ++i)
        {
            for (j = 1; j <= n + 1; ++j)//枚举四个点组成的四边形的面积
            {
                area[i][j] = GetArea(p[i][j], p[i + 1][j], p[i + 1][j + 1], p[i][j + 1]);
                if (ans < area[i][j])//找出最大的网格面积
                {
                    ans = area[i][j];//更新
                }
            }
        }
    }
    int main()
    {
        int i;
        while (scanf("%d", &n) != EOF && n)
        {
            memset(a, 0, sizeof(a));
            memset(b, 0, sizeof(b));
            memset(c, 0, sizeof(c));
            memset(d, 0, sizeof(d));
            memset(p, 0, sizeof(p));
            for (i = 1; i <= n; ++i)
            {
                scanf("%lf", &a[i].x);
                a[i].y = 0.0;
            }
            for (i = 1; i <= n; ++i)
            {
                scanf("%lf", &b[i].x);
                b[i].y = 1.0;
            }
            for (i = 1; i <= n; ++i)
            {
                scanf("%lf", &c[i].y);
                c[i].x = 0.0;
            }
            for (i = 1; i <= n; ++i)
            {
                scanf("%lf", &d[i].y);
                d[i].x = 1.0;
            }
            ans = 0;
            GetPoints();
            GetAreas();
            printf("%lf\n", ans);
        }
        return 0;
    }
  • 相关阅读:
    Sql Server常见错误
    sql server复制需要有实际的服务器名称才能连接到服务器
    学习WCF必备网址
    利用Httphandler、UserControl 输出HTML片段
    SQL点滴—性能分析之执行计划
    我的WCF开发框架简化版及基于NET.TCP传输方式的实现
    数据库优化建议
    实现jQuery扩展总结
    【模板】zkw线段树
    洛谷 P1960 列队
  • 原文地址:https://www.cnblogs.com/lidaojian/p/2481304.html
Copyright © 2020-2023  润新知