zoukankan      html  css  js  c++  java
  • Fishnet(几何)

    http://poj.org/problem?id=1408

    题意:给出 a1 a2 ... an
                   b1 b2 ... bn 
                   c1 c2 ... cn 
                   d1 d2 ... dn 这些点,求这些对应点连线形成的小四边形的最大面积。

    思路:将所有的交点求出,同已知点一起存入二维矩阵中,枚举每个小四边形,求出其面积,找出最大的即可。 

      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <math.h>
      4 const double eps=1e-8;//设置精度
      5 #define max1(a1,b1) (double)a1-(double)b1>eps?(double)a1:(double)b1
      6 struct point//
      7 {
      8     double x;
      9     double y;
     10 } map[120][120];//用二维数组的结构体存储所有的点
     11 struct line//线
     12 {
     13     double a,b,c;
     14 };
     15 line getline(point p1,point p2)//由两点求直线ax+by+c=0
     16 {
     17     line tmp;
     18     tmp.a = p1.y-p2.y;
     19     tmp.b = p2.x-p1.x;
     20     tmp.c = p1.x*p2.y-p2.x*p1.y;
     21     return tmp;
     22 }
     23 point getIntersect(line L1,line L2)//求两直线交点
     24 {
     25     point tmp;
     26     tmp.x = (L1.b*L2.c-L2.b*L1.c)/(L1.a*L2.b-L2.a*L1.b);
     27     tmp.y = (L1.c*L2.a-L2.c*L1.a)/(L1.a*L2.b-L2.a*L1.b);
     28     return tmp;
     29 }
     30 double area_polygon(int n,point* p)//求多边形面积
     31 {
     32     double s1=0,s2=0;
     33     int i;
     34     for (i=0; i<n; i++)
     35     {
     36         s1+=p[(i+1)%n].y*p[i].x;
     37         s2+=p[(i+1)%n].y*p[(i+2)%n].x;
     38     }
     39     return fabs(s1-s2)/2;
     40 }
     41 int main()
     42 {
     43     int n;
     44     while(~scanf("%d",&n)&&n)
     45     {
     46         double Max = eps;
     47         double a,b,c,d;
     48         map[0][0].x = 0;//初始化已知的四个顶点的坐标
     49         map[0][0].y = 0;
     50         map[0][n+1].x = 1;
     51         map[0][n+1].y = 0;
     52         map[n+1][0].x = 0;
     53         map[n+1][0].y = 1;
     54         map[n+1][n+1].x = 1;
     55         map[n+1][n+1].y = 1;
     56         for (int j = 1; j <= n; j++)//输入a1 a2 ... an,并存储其坐标
     57         {
     58             scanf("%lf",&a);
     59             map[0][j].x = a;
     60             map[0][j].y = 0;
     61         }
     62         for (int j = 1; j <= n; j++)//输入b1 b2 ... bn,并存储其坐标
     63         {
     64             scanf("%lf",&b);
     65             map[n+1][j].x = b;
     66             map[n+1][j].y = 1;
     67         }
     68         for (int i = 1; i <= n; i++)//输入c1 c2 ... cn,并存储其坐标
     69         {
     70             scanf("%lf",&c);
     71             map[i][0].x = 0;
     72             map[i][0].y = c;
     73         }
     74         for (int i =1; i <= n; i++)//输入d1 d2 ... dn,并存储其坐标
     75         {
     76             scanf("%lf",&d);
     77             map[i][n+1].x = 1;
     78             map[i][n+1].y = d;
     79         }
     80         for (int i = 1; i <= n; i++)
     81             for (int j = 1; j <= n; j++)
     82             {
     83                 line L1 = getline(map[i][0],map[i][n+1]);//枚举所有可相交的直线
     84                 line L2 = getline(map[0][j],map[n+1][j]);
     85                 point tmp = getIntersect(L1,L2);//求这两条线的交点
     86                 map[i][j].x = tmp.x;//将求得的交点坐标存入相应的数组中
     87                 map[i][j].y = tmp.y;
     88 
     89             }
     90         point p[5];//存储四边形的顶点
     91         for (int i = 1; i <= n+1; i++)
     92         {
     93 
     94             for (int j = 1; j <= n+1; j++)//枚举所有四边形右上角的顶点
     95             {
     96                 p[0] = map[i][j];//以map[i][j]为右上角的顶点组成的四边形的各点
     97                 p[1] = map[i][j-1];
     98                 p[2] = map[i-1][j-1];
     99                 p[3] = map[i-1][j];
    100                 double s = area_polygon(4,p);//求四边形的面积
    101                 Max = max1(s,Max);//求最大面积
    102             }
    103 
    104         }
    105         printf("%.6f
    ",Max);
    106     }
    107     return 0;
    108 }
    View Code
  • 相关阅读:
    hadoop2.2编程:MRUnit测试
    TestLinkConverter编程纪要
    Redisson的分布式锁的简单实现
    Jmeter工具使用
    JVM的学习(三)
    Mybatis需要注意的地方及知识点
    JVM学习二(JAVA的四种引用)
    mysql的引擎
    JVM学习一(JVM的内存结构和垃圾回收)
    JDk1.8HashMap的源码分析
  • 原文地址:https://www.cnblogs.com/lahblogs/p/3375020.html
Copyright © 2011-2022 走看看