zoukankan      html  css  js  c++  java
  • hdu 4709 Herding

    Problem Description
    Little John is herding his father's cattles. As a lazy boy, he cannot tolerate chasing the cattles all the time to avoid unnecessary omission. Luckily, he notice that there were N trees in the meadow numbered from 1 to N, and calculated their cartesian coordinates (Xi, Yi). To herding his cattles safely, the easiest way is to connect some of the trees (with different numbers, of course) with fences, and the close region they formed would be herding area. Little John wants the area of this region to be as small as possible, and it could not be zero, of course.
     

     

    Input
    The first line contains the number of test cases T( T<=25 ). Following lines are the scenarios of each test case.
    The first line of each test case contains one integer N( 1<=N<=100 ). The following N lines describe the coordinates of the trees. Each of these lines will contain two float numbers Xi and Yi( -1000<=Xi, Yi<=1000 ) representing the coordinates of the corresponding tree. The coordinates of the trees will not coincide with each other.
     

     

    Output
    For each test case, please output one number rounded to 2 digits after the decimal point representing the area of the smallest region. Or output "Impossible"(without quotations), if it do not exists such a region.
     

     

    Sample Input
    1
    4
    -1.00 0.00
    0.00 -3.00
    2.00 0.00
    2.00 2.00
     
    Sample Output
    2.00
     

     

    Source
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #define inf 0x3f3f3f3f
     6 #define exd 1e-8
     7 using namespace std;
     8 struct node
     9 {
    10     double x,y;
    11 };
    12 node dd[111];
    13 int n;
    14 double solve(node a,node b,node c)
    15 {
    16     return fabs((a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y))/2.0;
    17 }
    18 
    19 int main()
    20 {
    21     int t;
    22     scanf("%d",&t);
    23     while(t--)
    24     {
    25         scanf("%d",&n);
    26         for(int i=0;i<n;i++)
    27         {
    28             scanf("%lf%lf",&dd[i].x,&dd[i].y);
    29         }
    30         if(n<=2)
    31         {
    32             printf("Impossible
    ");
    33             continue;
    34         }
    35         double MinRes=inf;
    36         for(int i=0;i<n;i++)
    37         {
    38             for(int j=i+1;j<n;j++)
    39             {
    40                 for(int k=j+1;k<n;k++)
    41                 {
    42                     double ans=solve(dd[i],dd[j],dd[k]);
    43                     if(ans<MinRes && ans>exd)
    44                     {
    45                         MinRes=ans;
    46                     }
    47                 }
    48             }
    49         }
    50         if(fabs(MinRes-inf)<exd)
    51         {
    52             printf("Impossible
    ");
    53         }
    54         else
    55         {
    56             printf("%.2lf
    ",MinRes);
    57         }
    58     }
    59     return 0;
    60 }
    View Code

    比赛的时候错了好多次,以为题目意思理解错了,比完了,一查解题报告,没想到时自己的三角形求面积那里有错误,不过到现在我还是不知道,为什么用海伦公式(算出三条边的长)会WA,

    不过吃一堑长一智,记得下次求N边形面积的时候,果断使用叉积,避免没必要的精度损失,导致没必要的WA,谨记谨记啊!!

  • 相关阅读:
    ble学习笔记五------------独立按键查询方式
    android深度搜索学习笔记四(硬件抽像hal第一部分)
    ble学习笔记四---------------------控制lcd
    ble学习笔记二-----------------点亮led
    ble学习笔记三---------------------------控制led
    ble学习笔记一---------开发环境搭建
    TI 蓝牙协议栈视频学习笔记
    spring与quart整合实现任务调度_学习笔记
    mongod学习笔记一
    maven学习笔记二
  • 原文地址:https://www.cnblogs.com/ouyangduoduo/p/3310868.html
Copyright © 2011-2022 走看看