HDU 4709 Herding

 Herding

                                      Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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


  题意:给出你n个点的坐标,让你求出任意几个点围成的最小面积
  分析:既然叫求围成的最小面积,最先想到是三角形的面积,因为任意的多边形都可以分成三角形
  给出三点坐标求三角形的面积
   设A(x1,y1),B(x2,y2),C(x3,y3) 
    由A-->B-->C-->A 按逆时针方向转。(行列式书写要求) 
    设三角形的面积为S 
    则S=(1/2)*(下面行列式) 
   |x1 y1 1| 
  |x2 y2 1| 
  |x3 y3 1| 
  S=(1/2)*(x1y2*1+x2y3*1+x3y1*1-x1y3*1-x2y1*1-x3y2*1) 
  即用三角形的三个顶点坐标求其面积的公式为: 
  S=(1/2)*(x1y2+x2y3+x3y1-x1y3-x2y1-x3y2)
   于是枚举出所有的求出最小即可。


#include<stdio.h>
#include<math.h>
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,i,j,k;
        double a[105],b[105],min=9999999,s = -1;
        scanf("%d",&n);
        for(i = 1 ; i <= n ; i ++)
            scanf("%lf%lf",&a[i],&b[i]);
        if(n <= 2)               //小于3说明围不成三角形
         {
             printf("Impossible\n");
             continue;
         }
         else
        {

            for(i = 1 ; i <= n-2 ; i ++)     //求出所有的三角形面积,求出最小
                for(j = i+1 ; j <= n-1 ; j ++)
                    for(k = j+1 ; k <= n ; k ++)
                    {
                        s =fabs( 0.5*(a[i]*b[j]-a[j]*b[i]+a[j]*b[k]-a[k]*b[j]+a[k]*b[i]-a[i]*b[k])); 
                        if(s!=0&&s < min)
                            min = s;
                    }
        }
        if(min==9999999)    
          printf("Impossible\n");
       else
            printf("%.2lf\n",min);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值