AYIT2016省赛集训第五周 A - Herding(三角形面积)

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
 


分析:

题目就是说有一些已知坐标的点,从中任意找出几个点,找围成的区域面积最小是多少,显而易见,三点组成的三角形是面积最小的,所以只需比较三角形的大小就行;

三角形面积可以用行列式计算得出:s=(x.a*y.b+x.b*z.a+y.a*z.b)-(z.a*y.b+y.a*x.b+x.a*z.b);

但是当点数小于3或者三点组不成三角形时,输出Impossible;

#include<stdio.h>
#define ma 0x3f3f3f3f
struct P
{
    double a,b;
} T[110];
double area (P x,P y,P z )//用行列式计算三角形面积
{
    double s=(x.a*y.b+x.b*z.a+y.a*z.b)-(z.a*y.b+y.a*x.b+x.a*z.b);
    return s>0?s:-s;//如果小于0返回绝对值
}
int main()
{
    int t,m,i,j;
    double s,n;
    scanf("%d",&t);
    while(t--)
    {
        s=ma;
        scanf("%d",&m);
        for(i=0; i<m; i++)
            scanf("%lf%lf",&T[i].a,&T[i].b);
        for(i=0; i<m-2; i++)//循环求出部分和
            for(j=1; j<m-1; j++)
                for(int k=2; k<m; k++)
                {
                    n=area(T[i],T[j],T[k])/2;
                    if(n<s&&n>1e-6)//n小于s且n不等于0,一位是浮点型,所以是1e-6
                        s=n;
                }
        if(m<3||s==ma)//如果没法组成三角形
            printf("Impossible\n");
        else
            printf("%.2lf\n",s);
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值