您的位置:首页 > 其它

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

2014-08-15 10:27 183 查看

Herding

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


[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

1

4

-1.00 0.00

0.00 -3.00

2.00 0.00

2.00 2.00

[align=left]Sample Output[/align]

2.00

[b]题意:[/b]

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

套一个模版

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

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

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

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

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

#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\n");
else
printf("%.2lf\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: