Herding(hdu4709)三点运用行列式求面积

Herding

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1553 Accepted Submission(s): 440


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
 
 

题意:

告诉你很多个点的坐标,让你用这些点来求面积最小的三角形的面积。

套一个模版

通过三角形的顶点作坐标轴的平行线,把三角形围在一个矩形内,
该三角形的面积等于这个矩形面积减去两个直角三角形的面积
(三角形的一条边与坐标轴平行)或三个直角三角形的面积(三角形的边都
不与坐标轴平行),把式子写成行列式形式就得出这个公式了。

这样的,实际上用2阶就可以了(3阶那个写出来可以化成2阶)
比如有三个点(x1,y1),(x2,y2),(x3,y3)
那么用下面这个行列式
| x1-x3 y1-y3|
| x2-x3 y2-y3|
可以算一个值a出来
则S=1/2*|a|
记得一定要把a取绝对值
利用行列式的运算法则
S=(1/2)*(x1y2+x2y3+x3y1-y1x2-y2x3-y3x1)

假设空间三点A(x1,y1,z1) B(x2,y2,z2) C(x3,y3.z3) 那么S=1/2向量AB×向量BC

ps:http://acm.hdu.edu.cn/showproblem.php?pid=4709

转载请注明出处:http://www.cnblogs.com/yuyixingkong/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define maxn 1e10
using namespace std;

struct point
{
    double x,y;
};


double area(point a,point b,point c)    //运用行列式求面积
{
    double temp=((a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y));
    return temp<0? -temp:temp;//取绝对值;
}
int main()
{
    double ans;
    point p[105];
    int T,n,i,j,k;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(i=0;i<n;i++)
            scanf("%lf%lf",&p[i].x,&p[i].y);
        ans=maxn;
        for(i=0;i<n-2;i++ )
        {
            for(j=i+1;j<n-1;j++)
            {
                for(k=j+1;k<n;k++)
                {
                    double temp=area(p[i],p[j],p[k])/2.0;
                    if(ans>temp&&temp>1e-8)//精度控制
                        ans=temp;
                }
            }
        }
        if(n<3||ans<1e-4||ans==maxn)
            printf("Impossible
");
        else
            printf("%.2lf
",ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/yuyixingkong/p/3914129.html